The champions hit the bar in the opening minute when a long punt downfield picked out Callum Paterson only for his volley to crash back off the frame of the goal.
But Cowdenbeath, who lost 10-0 to their opponents earlier this season, took a shock lead on 14 minutes when Colin Marshall found the bottom corner from the edge of the area.
Hearts rallied well and Kenny Anderson’s half-volley forced Robbie Thomson into an acrobatic save before they levelled after 32 minutes when Jamie Walker’s cross was headed home from close range by the lively Paterson.
Cowdenbeath might have been back in front just before the break when Morgaro Gomis was robbed by Colin Nish, but he skied the ball over the bar after his initial shot had been parried by Jack Hamilton.
Walker then tested Thomson with a fierce drive from Walker’s layoff, while Osman Sow was then denied after excellent work from Sam Nicholson.
But with Hearts looking set to be frustrated, Walker was fouled inside the box, prompting McKay to dispatch from 12 yards and complete the fightback.