A stoppage-time header by Andrew Considine against Queen of the South clinched a Scottish Cup quarter-final spot for Aberdeen at Motherwell.
Rory Fallon helped to set up Fraser Fyvie to strike for the Dons' opener after 21 minutes at Palmerston Park.
Queens' Ryan McGuffie levelled from the spot after Youl Mawene had handled, then Nicky Clark hit the Dons' bar.
And, with the fifth-round replay heading for extra-time, Considine snatched the winner.
The Pittodrie side dominated the first half and got their reward when Fyvie pounced to score an opportunistic goal from close range.
Queens goalkeeper Lee Robinson pulled off a superb reaction save from Scott Vernon after great work from Mitch Megginson.
The home side had a sniff at goal when Sam Parkin fluffed his shot after meeting Daniel Carmichael's cross eight yards from goal.
Prior to half-time, winger Megginson limped off and was replaced with Daniel Uchechi, who was making his debut.
The recent signing showed superb skill to break free on the left wing before finding Vernon, but his shot lacked pace and power.
Fyvie was unlucky with a first-time volley from Mark Reynolds' long pass as Aberdeen tried to kill the game.
But the Doonhamers hauled themselves back into the contest just before the hour when McGuffie scored from the penalty spot after Mawene handled Nicky Clark's cross, a decision that infuriated the Aberdeen players.
A confident Queens team pushed forward and Clark crashed a 20-yard volley off the crossbar, then Parkin's curling shot skimmed the bar after sublime trickery from Danny Carmichael created the opening.
Clark was a constant menace for Queens and his driving run and cross caused chaos in the Aberdeen box after 81 minutes.
Parkin and Scott McLaughlin were both waiting to pounce, but Aberdeen midfielder Stephen Hughes produced a last-ditch clearance.
With extra-time looming, Kari Arnason picked out Considine at the back post with a perfect cross and the defender buried his header past Robinson from six yards.
Queen of the South manager Gus MacPherson: "It's a bitter disappointment to finish the game in that manner. I thought the players were outstanding in that second half and should have won the game.
"We went out giving a good account of ourselves.
"The manner of the goal at that stage of the game was cruel.
"We should have been in front. We had two or three really good opportunities on the counter-attack. We hit the woodwork twice.
"If we had won the game nobody would have had any complaints.
"There is a confidence within the group and a determination that they want to improve. They work so hard."
Aberdeen manager Craig Brown: "No-one deserved to score the goal more than Andy because he has been a brilliant captain this season.
"I can't say I was proud of the performance. I wasn't. We know we can play far better. Great credit to the fans for their continued support right to the end.
"We were dominant for the first half an hour but once Queen of the South scored from the penalty their tails were up.
"But we stole the goal at the end. I'm grateful for that.
"The home team put everything in to it. We had 10 first-team players out of action.
"With luck like that we'll win the cup! We look forward to the next round at Motherwell."