They lost goals early in both halves, the first a Jamie Walker penalty after three minutes and the second just four minutes after the turnaround to a Callum Paterson header.
The Dark Blues grabbed one back through a shot from defender Darren O’Dea then, remarkably given how badly they played before the break, they equalised on 61 minutes through substitute Paul McGowan.
Then, with nine minutes of stoppage time at the end of the game, the home team made it 3-2 through matchwinner Marcus Haber.
These three points are absolutely precious this for Dundee and they deserve enormous credit for picking themselves up off the floor in some style.
By the end, it was they who were dominating and they deserved the wonderful win.
Dundee midfielder Mark O’Hara started his first game since the reverse fixture against the Jam Tarts at Tynecastle in October, having done well as a sub at Celtic Park last Saturday.
For the visitors, boss Ian Cathro handed a debut to Krystian Nowak but the man who had caught Dundee United’s attention earlier in the week, Conor Sammon, had to make do with a place on the bench.
The big fear all day was that Storm Barbara would put the match in doubt but, despite the gusts blowing around Dens, there was no threat to the game.
The home team were up against it after just three minutes when the Edinburgh men took the lead with Walker’s penalty.
Kevin Holt was the culprit when he tripped Walker and the fouled player stepped up to score with ease from the spot.
The visitors’ fans thought the ball was in the Dundee goal again three minutes later but Nowak’s strike just flew wide and scraped the sidenetting on its way.
Hearts were in the driving seat and the hosts were struggling to get out of their own half.
John Souttar played in Walker and his shot hit the post then, on 19 minutes, Liam Smith nodded past when given a free header in front of goal.
The Dark Blues had to do something to get into the match and they finally had a decent chance on 23 minutes when Craig Wighton raced on to a long ball inside the box and half-volleyed back across the face of Hearts’ goal.
Wighton then won possession inside the area again and cut back to James Vincent , who fired over the bar.
At least they were having a go now but they almost lost a second on 34 minutes.
They were rescued by a great save from keeper Scott Bain, who showed excellent reflexes to keep out a header from Paterson that had looked a goal all the way.
There was some dodgy defending from the Dens men three minutes later that allowed Walker to go clear but he could only chip into Bain’s arms.
Three minutes before the break, the visitors tried again but Bain’s hands were strong and pushed away Souttar’s drive.
The home team were booed off at half-time and that wasn’t a surprise given the poor performance.
The hosts made a defensive change at the break, replacing Kevin Gomis with Julen Etxabeguren.
However, they lost another goal at the start of a half when, with just four minutes of the second period gone, Paterson headed home from a Walker freekick.
Home boss Paul Hartley then brought on McGowan for O’Hara on 53 minutes and soon they had a goal back.
Just a minutes later, a low drive from O’Dea nearly burst the net after an intricate move involving McGowan, Haber and Wighton saw the striker’s blocked shot fall for the former Celtic defender, who warmed up the Dundee supporters with a decent strike.
The game was stopped for a lengthy spell as Jam Tarts player Don Cowie received treatment on the pitch following a slash with Bain.
He was carried away on a stretcher and the Tynecastle men brought on Robbie Muirhead.
The crowd was announced as 6,160, including 1,400 from Edinburgh, and they all deserved credit for coming out on such a chilly night just before Christmas.
And the home fans were on their feet again on 61 minutes when McGowan stabbed the ball into the net from close range after good play from Vincent.
It was a fine finish from the substitute, who gave Hamilton no chance, and the Dark Blues smelled blood.
They could have snatched the lead in the last minute of regulation time when Wighton’s header was saved by Bain.
The board went up to say there would be nine minutes of additional time .
That played into Dundee’s hands and up popped Haber to head home Tom Hateley’s freekick and make it 3-2. What a comeback!
Dundee: Bain, Holt, Vincent, O’Dea, Hateley, O’Hara (McGowan 53), Haber, Gadzhalov, Kerr, Wighton, Gomis (Etxabeguren 46). Subs not used: Mitchell, Williams, Ross, El Bakhtaoui, Loy.
Hearts: Hamilton, Paterson, Rossi, Kitchen, Walker (Watt 90+1), Djoum, Souttar, Cowie (Muirhead 65), Nowak, Johnsen, Smith. Subs not used: Noring, Rherras, Ozturk, Sammon, Zanatta.
Referee: Kevin Clancy.