The Dons started the brighter of the two teams, with Cosgrove denied only by Keith Watson's intervention on the line after Niall McGinn's low cross had been pushed into his path by goalkeeper Ross Laidlaw.
McGinn was involved in everything good for Aberdeen but he somehow blazed over on the quarter hour after being picked out by Leigh.
And Stewart was, unfortunately, involved in the build-up to the opening goal after 34 minutes.
The striker was penalised for a handball despite being nudged onto the ball by Funso Ojo.
A training ground free-kick routine saw McGinn send the ball wide to Hedges whose right-wing cross was headed home by Leigh from 10 yards.
Three minutes later it was 2-0. Laidlaw seemed in control as he went to gather a through ball on the right edge of his penalty area, but Lewis Ferguson had not given up on it and managed to get there a split second before the keeper, who bundled the Dons man over.
Referee Nick Walsh paused before awarding the spot-kick but it was a clear penalty and Cosgrove made no mistake in firing the ball into the bottom left corner.
They added a third just five minutes after the interval with McGinn again involved.
His deep cross from the left evaded Cosgrove but was met by the onrushing Hedges who turned home from close range.
The visitors were handed an immediate lifeline as Ojo was adjudged to have blocked a shot with his hand.
It seemed a harsh award and the ensuing booking added insult to injury, but McKay's effort came back off the upright.
Hedges was now playing with confidence and Aberdeen should have been further clear when the Welshman left Richard Foster for dead and crossed for McGinn at the near post, only for the Northern Irishman to flick wide.
County were still a threat on the break and Stewart was unlucky to see his 70th-minute shot rattle the upright.
Hedges could have netted again with 10 minutes remaining as the home side swept through their opponents' defence but the winger saw his low effort cleared away by Laidlaw.