Stephen Ireland poked home the winner almost five minutes into added time to save the Republic of Ireland's blushes in their Euro 2008 Group D qualifier.
Republic players engulf Stephen Ireland after his late winner
Robbie Keane, Richard Dunne and Kevin Kilbane went close in the first half before Kilbane headed home from close range on 49 minutes for the opener.
Michele Marani took advantage of a defensive mix-up to scramble home a sensational equaliser after 87 minutes.
Anthony Stokes fired wide before Ireland's goal dashed San Marino hopes.
Steve Staunton's side struggled to break down the part-timers' defence in the first period and goalkeeper Aldo Simoncini held an early right wing cross from Damien Duff.
Keane ran through on goal on 15 minutes but his right foot shot went wide of the post and on the half hour he was off target again with a long range effort.
Dunne headed over from a Duff corner on 39 minutes and Kilbane fired wide of the post a minute later as the resolute home defence performed stoically.
Kilbane eased Irish nerves when he netted with a powerful header soon after the interval after Ireland had nodded on a Steve Finnan cross.
Duff was proving a livewire on the wing but no Irish player could get the vital touch to a dangerous cross from the right.
Simoncini saved well from Stephen Hunt just a minute after the striker came on as a substitute and the keeper then denied Keane after a sharp turn and shot by the Tottenham striker.
A defensive mix-up between goalkeeper Wayne Henderson and Dunne allowed Marani to roll the ball into an empty net and the home side sensed the opportunity of a sensational draw.
Stokes was off target after a Keane cutback before Ireland stabbed the ball in in a crowded penalty area after a corner.
San Marino: Aldo Simoncini, Carlo Valentini, Manuel Marani, Albani, Davide Simoncini, Muccioli, Bonini (Vannucci 76), Domeniconi (Bugli 88), Michele Marani, Selva, Gasperoni (Andreini 66).
Subs Not Used: Federico Valentini, Ciacci, Nanni, Vitaioli.
Booked: Davide Simoncini, Selva.
Goals: Manuel Marani 86.
Rep of Ireland: Henderson, Finnan, Dunne, O'Shea (McShane 45), Harte (Hunt 74), Duff, Carsley, Ireland, Kilbane, Long (Stokes 80), Keane.
Subs Not Used: Colgan, Alan Quinn, Keogh, Gibson.
Goals: Kilbane 49, Ireland 90.
Ref: Peter Rasmussen (Denmark).