The Englishman opened the scoring early on, but it was a quiet affair until the game exploded into life in the final quarter.
But Aaron Hickey picked up a second booking to see the visitors go down to 10 men, and a minute later Cosgrove scored from the spot to level matters. Ryan Hedges then fired in his first Aberdeen goal to earn the home side the points.
It was evident early on that the game was going to be a physical affair with the home faithful claiming vociferously for a penalty as Cosgrove went down under pressure from Michael Smith. It seemed a decent shout, but referee Don Robertson waved play on.
However, the Dons were in front shortly after as Christophe Berra misjudged the flight of an Andrew Considine ball over the top.
Goalkeeper Zdenek Zlamal did not know whether to hare out or drop back onto his line, and Cosgrove took full advantage to slide an angled shot past the Czech for his seventh goal in five games.
Berra came close to making amends after 20 minutes as he connected well with a diving header from Andrew Irving's corner, but Joe Lewis got a strong hand to the ball to divert over the crossbar.
Hearts were dealt an injury blow with John Souttar forced off before the interval, to be replaced by Clevid Dikamona, but it was another defender, Craig Halkett, who came close to levelling the scores on the stroke of half-time.
Another Irving set-piece found the former Livingston man at the back post, but he could not generate sufficient contact on his header to trouble the goal.
But Hearts did level midway through the second half. A Smith throw-in was flicked on by Halkett, and substitute Naismith threw himself in at the near post to head home from close range.
And they were in front after 76 minutes. Uche Ikpeazu slipped in Walker, who got beyond the Aberdeen backline and scored with a finish reminiscent of Cosgrove's first-half effort.
But they were soon reduced to 10 men as Hickey was sent packing for a second booking, the latter for chopping down Niall McGinn outside the area.
The resulting free-kick saw Dikamona send McGinn flying again, and from the penalty spot Cosgrove made no mistake.
And the Dons were back in front with five minutes remaining as Hedges blasted in from 12 yards after McGinn flicked a Greg Leigh cross into the Welshman's path.