Jayden Stockley set Aberdeen on the way to their second Ladbrokes Premiership victory of the season when he headed home against Dundee at Dens Park three minutes after coming off the bench.
The former Bournemouth striker converted Jonny Hayes' 77th-minute cross, and Kenny McLean's 88th-minute penalty sealed a 3-1 win to send Aberdeen into fourth place above Rangers on goal difference ahead of the pair's meeting at Pittodrie next weekend.
Kevin Holt had given Dundee a 13th-minute lead but James Maddison levelled six minutes later with a long-range strike.
Darren O'Dea brushed off a foot knock to start beside Julen Etxabeguren in the heart of the Dundee defence and he was involved early on as he cleared Anthony O'Connor's header off the line.
Aberdeen continued to press in the early stages but they were caught out by a sweeping counter-attack.
Dundee worked the ball from right to left before Nick Ross flicked Paul McGowan's forward pass into the path of left-back Holt, who slotted into the corner from 15 yards.
Aberdeen levelled quickly.
On-loan Norwich midfielder Maddison had already looked dangerous in his role off striker Adam Rooney on several occasions and he made himself space to shoot from 22 yards with a clever turn.
The strike was low and hard but Scott Bain got down to it and let it squirm through his body.
Aberdeen continued to have more possession with Jonny Hayes giving right-back Cammy Kerr some difficult moments, especially after the 21-year-old was booked for a pull on the Dons winger.
But Dundee looked dangerous on the break and Faissal El Bakhtaoui curled just wide after one surge forward.
The hosts were handed the first chance of the second half when a loose pass from Andrew Considine set up Paul McGowan, but the midfielder was running away from goal when he shot past the near post.
Aberdeen midfielder Graeme Shinnie soon smashed the ball off the bar from 22 yards and Maddison had a good chance when Niall McGinn sent him clear with a first-time pass
But Bain was off his line quickly to block the former Coventry player's attempt.
Considine got away with a clumsy penalty-box barge on El Bakhtaoui before Mark O'Hara shot just over the Aberdeen bar from long range.
Aberdeen manager Derek McInnes brought on Wes Burns and Stockley to play up front in the 74th minute and his change soon paid off.
Hayes got down the right wing and sent over an inviting cross which Stockley ran on to and headed into the top corner from eight yards.
The Dons looked comfortable and McLean converted a spot-kick after Etxabeguren brought down Burns before O'Hara was sent off in injury-time for a second booking after a foul on Shinnie.