Sam Cosgrove clinched Aberdeen's place in the quarter-final of the Betfred Cup thanks to his extra-time winner against Dundee at Dens Park.
Derek McInnes' side were within minutes of exiting and suffering a fourth straight defeat until Andrew Considine's stoppage-time equaliser cancelled out Danny Johnson's 43rd-minute penalty.
The Dons, who won this competition in 2014, found themselves outplayed by James McPake's Championship hopefuls for long spells of this last-16 encounter, before Cosgrove's 103rd minute header saw them advance.
Aberdeen arrived on Tayside on the back of a three-game losing streak, with their latest defeat being last Thursday's 2-0 reverse at the hands of Croatian outfit Rijeka at Pittodrie.
It meant they tumbled out of the Europa League third qualifying round having lost the week before in Croatia 2-0.
Not surprisingly they began tentatively as their large travelling support hoped for a return to winning ways.
It was a robust start with Cosgrove becoming the first player to be booked in 16 minutes for lifting his hand to Cammy Kerr.
Midway through the first-half, Dundee youngster Finlay Robertson won possession just inside the opposition half before striding forward.
As the Dons defence back off the teenager, Robertson unleashed a low 20-yard effort which came bouncing back off Joe Lewis' left-hand post.
Dundee were appealing for a penalty in the 36th minute when Kerr's whipped cross appeared to strike the hand of Funso Ojo, but referee Willie Collum saw nothing untoward.
However, they were not denied two minutes before the interval when Considine hauled Jordan Forster to the ground from Kerr's free-kick.
Johnson, who converted two penalties in their 2-2 opening-day draw at Dunfermline, stepped up to make it 1-0 with a well-drilled effort which sent Lewis the wrong way.
Dundee remained in the ascendancy after the break as Shaun Byrne went on a solo run deep into the opposition penalty box, only to see his left-foot shot trundle past the post.
Aberdeen's attacks were sparse, although Jon Gallagher did have an opportunity in the 53rd minute but his shot went straight into the arms of Dundee goalkeeper Jack Hamilton, while Cosgrove should have done better from close-range shortly afterwards.
The hosts, though, looked the more potent when Declan McDaid's 78th minute drive fizzed just over Lewis' crossbar.
However, the Dons levelled in the first minute of stoppage time when Considine turned home from point-blank range after Hamilton's spectacular save from Greg Leigh's thumping header.
Cosgrove then struck the winner with a textbook downward header from Leigh's looping header back across Hamilton to send the Red Army behind the goal wild with delight.