Substitute Stefan Radosavljevic popped up with an 89th minute winner at Oriel Park as Sligo Rovers leapfrogged Dundalk into third place in the SSE Airtricity League Premier Division.
It looked as though the game was heading for a draw after Max Mata's opener was cancelled out by Rayhann Tulloch a minute before half-time.
However, John Russell’s side earned a deserved win when Faroese international Radosavljevic, a target for the Lilywhites under former boss Filippo Giovagnoli in 2021, weaved his way into the box to fire his first goal for Sligo a minute from the end.
The victory sees the Bit O'Red move into the top three with Dundalk dropping to sixth, as Stephen O’Donnell suffered his first back-to-back home defeats as Lilywhites boss following on from the previous week’s 4-0 defeat by Shamrock Rovers.
A warning sign of what was to come from the visitors arrived after just five minutes when Will Fitzgerald found space on the left to cross for Mata but his first-time attempt saw him kick the ball into his own face as the chance went abegging.
Mata then came to his side's rescue two minutes later at the other end when he blocked John Martin’s header on the line after he had been picked out by Connor Malley’s free-kick.

Dundalk then had another big chance on 11 minutes when O’Kane was allowed to run at the Sligo defence after Malley had nicked the ball off Frank Liivak but the teenager’s effort was touched over by Luke McNicholas.
The Bit O'Red then hit the front on 16 minutes with Fitzgerald and Mata combining once again.
This time the winger fizzed a low ball in from the left with Mata timing his run to perfection to race between the Dundalk centre halves to tap home his sixth goal of the season to date.
Having opened the scoring, the New Zealander almost turned provider three minutes later when he hooked the ball back to Niall Morahan at the edge of the box only for the midfielder to fire just wide with his first-time volley.
Dundalk began to apply most of the pressure after that with Archie Davies having a dipping shot from range tipped over by McNicholas on 32 minutes.
From the resultant short corner, O’Kane’s delivery found Malley at the back post but his downward header was comfortable for the keeper.
The Louth men would get back on level terms a minute before the break though with Tulloch winning his side a penalty after being adjudged to have been fouled by John Mahon after a brilliant piece of skill had seen him beat Liivak on the left.
The on-loan West Brom player then stepped up to take the spot kick but despite being denied by McNicholas, he was on hand to finish off the rebound to make it 1-1.
Reece Hutchinson went close to restoring the visitors’ lead within four minutes of the restart when he flashed an effort across the face of goal from the left hand corner of the penalty box.
Sligo would threaten again on 67 minutes when Liivak was given far too much time and space to drift in from the right onto his left foot but fortunately for the home side his effort was straight at Nathan Shepperd.
Dundalk then threatened for the first time in the half five minutes later when Malley broke in on the left but just as he was about to pull the trigger Mahon got a vital touch to deny him.
The visitors then went close again on 82 minutes when Kailin Barlow’s corner was flicked on at the near post by Fitzgerald but his header flashed across the face of goal before being hooked to safety.
The winner would arrive a minute from time, however, as Dundalk were punished for overplaying at the edge of their box with Radosavljevic taking advantage by picking up a loose pass and weaving his way into the area before slotting past Shepperd.
Dundalk: Nathan Shepperd; Archie Davies, Hayden Muller, Andy Boyle, Darragh Leahy; Johannes Yli-Kokko (Greg Sloggett 60), Alfie Lewis; Rayhann Tulloch (Robbie McCourt 86), Connor Malley, Ryan O’Kane (Daniel Kelly 61); John Martin
Sligo Rovers: Luke McNicholas; Frank Liivak, John Mahon, Nando Pijnaker, Reece Hutchinson; Lukas Browning Lagerfeldt, Niall Morahan (Greg Bolger 77); Fabrice Hartmann (Kailin Barlow 77), Bogdan Vastsuk (Stefan Radosavljevic 68), Will Fitzgerald; Max Mata.
Referee: Damien MacGraith (Mayo).