Magennis, who had endured a miserable afternoon, scored with two minutes left as the Dons showed great character to wipe out the Pars’ two-goal lead in the SPL on Saturday.
Andrew Considine opened the scoring in first-half injury-time but the hosts responded after the break with strikes from Andy Barrowman, David Graham and Liam Buchanan.
Alex Keddie gave Aberdeen hope with his own goal before Magennis made amends for missing a series of earlier chances.
Despite the blustery conditions, both sides served up a first period full of penalty box action by keeping the ball on the ground.
Aberdeen went ahead in first half injury time when Considine powered in a header at the back post after connecting with Rob Milson’s corner.
Craig Brown opted to replace Rory Fallon at the interval with Darren Mackie.
And the Aberdeen manager was relieved Fraser Fyvie did not need to come off just five minutes into the second period as the midfielder survived a two-footed Paddy Boyle challenge that earned the Pars full-back a yellow card.
From the free-kick, Milsom, who teed up Considine for the goal, swung a left-footed effort wide of target.
Dunfermline levelled in the 54th minute with a corner from the same part of the pitch that saw the Dons find the net.
Aberdeen goalkeeper David Gonzalez blocked Keddie’s downward header after the defender met Willis’ set-piece but Barrowman pounced to sweep in the rebound.
The Pars then completely turned the game around in the 62nd minute with a second goal from Graham.
The winger eased past Youl Mawene on the edge of the box and slammed a low right footed shot into the corner of the net past the outstretched Gonzalez.
Aberdeen wasted a great chance to equalise in the 69th minute through Magennis when he lashed a shot wide with Gallacher out of his goal and Dunfermline added a third seconds later.
Boyle’s low drive was blocked by Gonzalez and
substitute Buchanan rolled the ball into the net with his first touch of the game.
Magennis was having a torrid afternoon in front of goal after he failed to reduce the Dons’ arrears following a good block by Gallacher.
Latching on to Fyvie’s pass, Magennis tried to slip an effort past the keeper but an important touch by Gallacher saw the ball drift into the side netting.
Gallacher was enjoying his duel with the striker after saving another Magennis shot just moments later.
Dunfermline should have had a fourth in the 79th minute when Buchanan teed up the unmarked Barrowman in the box but the striker’s shot was pushed onto the post by Gonzalez before the ball was cleared.
Dunfermline defender Keddie ensured of a nervy finale for his team by heading the ball into his own net from Milsom’s corner just seconds later.
Aberdeen then levelled in the 88th minute through Magennis as he pounced on Gary Mason’s poor touch in the box and powered a low drive past Gallacher from 16 yards.
Aberdeen defender Mawene then headed another Milson corner just inches wide in injury time.