Kerr Waddell was Dundeeās hero with a stoppage time winner.
The young centre-back had put the Dark Blues ahead in the first half only for the home team to be pegged back thanks to a moment of madness from Sofien Moussa.
The striker hit the sort of back pass that will keep him awake at night to tee up Kyle Laffertyās equaliser.
Up until that point Dundee were in control of the game but it looked as if they would have to settle for a point.
In injury-time, however, Waddell powered home his second header from a corner to seal a dramatic win.
There were some significant alterations for Dundee on the back of last weekendās draw at Kilmarnock, with Darren OāDea, Mark OāHara, Moussa and Paul McGowan all starting. Cammy Kerr, A-Jay Lietch-Smith, Faissal El Bakhtaoui and Scott Allan were on the bench.
Neil McCann encourages his side to play out from the back but they almost got caught out doing just that early in the game and a Scott Bain clearance flew just past after Jamie Walker charged it down.
After 15 minutes a spell of Dundee pressure ended up with Waddell coming close with a header.
If this was a warning to Hearts of the big defenderās capabilities in the air, they didnāt heed it, as one minute before the break Waddell was back in the opposition box and heading home an outswinging Kevin Holt corner to make it 1-0 at half-time.
Although Dundee had the lead there were precious few chances for either side in the opening 45. The second period started a bit more promisingly, though, with Roarie Deacon forcing a save out of Jon McLaughlin with a shot on the angle.
At the other end an Isma Goncalves cross took a deflection and Bain had to be alert to stop it sneaking in at his near post.
The same man came even closer just before the hour mark when he rattled the bar with a volley that Bain couldnāt get close to.
The game was now coming alive and seconds later at the other end an OāHara cross into the box wasnāt cut out by John Souttar. Moussa should have taken advantage at the back post but his shot lacked the power to beat McLaughlin.
To say Hearts were gift-wrapped an equaliser on 71 minutes would be an understatement.
Under no pressure in the middle of the park Moussa hit a horribly misjudged back pass that went straight to Lafferty. The Northern Irishman still had plenty of work to do but took the ball round Bain and slotted the ball home from a tight angle.
To say McCann was furious with his centre-forward would also be an understatement. The Dundee boss let Moussa know what he thought of his decision-making and then threw his water bottle into the ground.
Hearts were now on top and Bain did well to save from Walker on 76 minutes and then again moments later.
In the 90th minute Lafferty was free wide in the box but his shot could only find the side-netting, then in injury time substitute A-Jay Leitch-Smith nearly found the bottom corner with an 18-yarder.
His shot was tipped round the post and from the corner Waddell headed in his winner.