Ash Taylor had levelled for Aberdeen on an afternoon which tested Celtic's mettle but ended in the predicted outcome.
The home side had forced much of the early pressure, but Celtic, as they are capable of, hit the hosts with a sucker-punch after 10 minutes.
Leigh Griffiths nudged Scott McKenna as the defender looked to clear an aerial ball in the right channel, and the visitors worked the ball across to James Forrest. His shot was well saved by Joe Lewis, but the rebound fell to McGregor who found the bottom corner.
Celtic were forced into an early change as Greg Taylor was forced off with a hamstring injury 25 minutes in and a minute later, Aberdeen were level.
Connor McLennan delivered from the right after the Dons recycled a Niall McGinn free-kick, and his cross was nudged on by Matty Kennedy for the unlikely figure of Taylor, who took a touch on the chest before firing past Fraser Forster.
That spurred the hosts on, and while it would be difficult to say they dominated, they certainly had the better openings.
Funso Ojo saw a long-range effort beaten away by Forster, with McGinn heading the loose ball into the side-netting.
McLennan picked up a booking for a dive as he went down in the presence of Christopher Jullien. The Celtic defender was perhaps fortunate to escape unpunished for a clumsy challenge on Curtis Main a minute from the interval.
Though clear-cut chances were few and far between, this was a lively contest that neither side was willing to give up s without a fight – and that perhaps suited the home side more after their torrid recent run.
However Jonny Hayes, the former Aberdeen winger, could have put Celtic in front after 74 minutes as he raced in on goal, only to be denied by the onrushing Lewis.
It fell then to Ajer to net the decider, but it was Odsonne Edouard's back-heel in the build-up that set everything in motion.
He found McGregor who passed the ball onto a subdued Forrest. The winger suddenly came into life, though, and picked out the unmarked Norwegian at the back post, and he stroked high past Lewis from 10 yards.
Edouard should have put the outcome beyond doubt when Forrest's cross from the right found him a yard out. With Lewis on the ground, the Frenchman somehow contrived to shoot straight at the goalkeeper from a yard out.