After a goalless 90 minutes and an equally fruitless period of extra-time, the match had to be settled by penalties – in which Gallacher saved from Scott Severin and Richard Foster to give his side a 4-2 victory.
The Pars acquitted themselves well in the first 90 minutes, Steven Bell going close twice with his head in the first half.
The Dons dominated extra-time but could find no way past a dogged Dunfermline defence.
The result means Jim McIntyre’s side will face Falkirk in next month’s semi-final.
Richard Foster replaced the injured Javan Vidal for Aberdeen, who made two changes to the side that beat Hamilton 1-0 on Saturday.
Darren Mackie also started in attack for the Dons ahead of Tommy Wright, who dropped to the bench.
The Pars made three changes to the side that lost 1-0 to Partick. Calum Woods returned from suspension to replace Greg Shields; Graham Bayne recovered from a head injury to take over from David Graham, and Jamie Mole also started – with Graeme Holmes missing out.
The home side should have opened the scoring within two minutes when Sone Aluko threaded a tantalising ball into the box, but Mackie failed to connect from seven yards.
Gallacher was then called into action to tip a Mark Kerr header over the bar a minute later as the Dons pressed for an early opener.
Nicky Phinn, who scored for the Pars when the sides drew 1-1 first time around, had a chance at the other end after 10 minutes – but his lob sailed harmlessly over the bar.
Bell showed his aerial ability after 17 minutes when he out-jumped Jamie Langfield, but his header went wide of the target.
Foster then tested Gallacher with an audacious drive from 35 yards that took a deflection en route to goal – but the Pars goalkeeper was equal to it.
Bell spurned another great opportunity to open the scoring after 33 minutes when he could not guide his free header from an accurate Alex Burke cross past Langfield.
Burke fired a weak shot from distance wide just before the half-time whistle.
Lee Mair came to the rescue for Aberdeen as he closed down a Bayne volley early in the second half, the Pars continuing to enjoy a healthy amount of possession against the SPL club.
At the other end, Chris Maguire’s acrobatic effort went well wide of the target after 55 minutes as the game ebbed and flowed.
Aberdeen had two appeals for a penalty in the space of a minute, first when Mair appeared to be held back in a packed penalty box before Maguire collided with Thomson. But referee Eddie Smith waved play on both times.
Mole stung the palms of Langfield with a fierce drive, before the injured Mair was withdrawn for Andrew Considine.
Jimmy Calderwood made his second change after 73 minutes when he brought Stuart Duff on for the ineffective Maguire.
Dunfermline made a double substitution as extra-time loomed, Phinn and Mole replaced by Rory Loy and Scott Muirhead.
With less than 10 minutes remaining, Loy had a shot at goal from 25 yards. But his effort drifted off target.
Considine had another attempt cleared by Bell on the line, before Aluko squandered a great chance to win the match as Stephen Glass cleared his low shot off the line.
Neither side managed to get the breakthrough in a scrappy opening period of extra-time. But Aberdeen looked the more likely, Smith and Aluko both having efforts blocked.
Calderwood brought on striker Tommy Wright for Aluko at the start of the second period.
The former Darlington forward almost made an instant impact, but his powerful header was saved by Gallacher.
The Dons kept pressing but could find no winner, so the tie had to be concluded by penalty kicks.
Gallacher was the hero with his two saves, and Graham Bayne slotted home the decisive penalty.