Dundalk had to dig deep to see off Waterford at Oriel Park, a 94th-minute Garry Comerford own goal finally sinking the Blues.
With the game deep in stoppage time, it looked like Stephen Kenny's side were on course for their fourth scoreless draw in just six games.
However, that all changed when Michael Duffy's vicious cross from the left ended up in Lawrence Vigouroux's net, the last touch appearing to come off Garry Comerford.
It was a goal greeted by a combination of euphoria and relief, the three points moving Dundalk ahead of the Blues into second place, one point drift of leaders Cork City.
Dundalk had the better of the chances and were kept at bay by Lawrence Vigouroux, the Chilean goalkeeper denying Karolis Chevedukas and Dane Massey in the first-half.
Massey squandered a gilt edged opening when he ballooned over the top in the second period and Patrick Hoban had a goal ruled out for offside as Dundalk pushed for a breakthrough.
The visitors defended well and they almost drew first blood when substitute John Martin held off Sean Hoare to bear down on goal but Gary Rogers stood tall to mark his 500th League of Ireland appearance with a sixth successive clean sheet.
It looked like that would be the only positive for Dundalk on the night but they kept going to win it right at the death.
Dundalk: Gary Rogers; Sean Gannon, Dan Cleary, Sean Hoare, Dane Massey; Chris Shields (Stephen O'Donnell 84), Karolis Chvedukas (Ronan Murray 71); Jamie McGrath (Marco Tagbajumi 79), Robbie Benson, Michael Duffy; Patrick Hoban.
Waterford: Lawrence Vigouroux; John Kavanagh, Kenny Browne, Dave Webster, Dylan Barnett; Derek Daly (John Martin 66), Stanley Aborah (Paul Keegan 76), Gavan Holohan, Garry Comerford, Sander Puri; Dean O’Halloran.
Referee: Rob Harvey.