The Tangerines got off to a flying start through Johnny Russell's second-minute opener before John McGlynn's visitors had Ryan Stevenson sent off for a foul on Gary Mackay-Steven midway through the first half.
Willo Flood made it 2-0 for McNamara's side on 62 minutes only for Michael Ngoo to pull one back for the Jambos in the 76th.
Despite their best efforts to force a late equaliser, it was the revitalised hosts who ran out winners with substitute Michael Gardyne sealing matters in the final minute.
The Tangerines did not have long to wait before breaking the deadlock against a stunned Hearts side.
John Rankin's cross was met by Russell, who took the ball off the toes of team-mate Stuart Armstrong before swivelling and finishing with his right foot low into Jamie MacDonald's right-hand corner from eight yards.
On 25 minutes Stevenson was sent off for a challenge on Mackay-Steven. The Hearts midfielder appeared to go in on high on the United winger, and referee Willie Collum showed no hesitation in producing a straight red card.
The hosts extended their lead in the 62nd minute thanks to Flood. A slick one-two involving Russell and Flood saw the former Celtic midfielder steady himself before slotting a perfect left-foot finish past the helpless MacDonald.
Hearts gave themselves some hope in the 76th minute when United failed to clear Fraser Mullen's corner and Ngoo pounce to drive a shot home from 12 yards.
But any hopes they had of a comeback ended in the 89th minute when Rankin released Jon Daly, who cut the ball back for substitute Gardyne to finish from point-blank range.