Kerr Waddell's first senior goals handed Craig Levein his first defeat since returning as Hearts manager.
Dundee's teenage defender headed home at the end of each half, with Kyle Lafferty having levelled for Hearts on 70 minutes.
Lafferty put the distress of his gambling addiction aside to restore parity at Dens Park and immediately ran to hug Levein and his assistant Austin MacPhee. Both men had been instrumental in supporting the striker before he went public on his gambling troubles.
However, Waddell's second header in stoppage time earned Dundee full points in a dramatic finish. His heroics would have somewhat pacified manager Neil McCann, too. He was throwing water bottles around the technical area after striker Sofien Moussa's ill-advised backpass had presented Lafferty with the equaliser.
Levein handed 16-year-old midfielder Harry Cochrane his league debut against a Dundee team seeking only their second home league win of the season. Again, a healthy travelling support of 2091 arrived on Tayside as Hearts continue their run of away matches whilst Tynecastle is redeveloped.
An early enforced change for the visitors saw the injured Aaron Hughes replaced by Prince Buaben, who slotted in at centre-back in a three-man defence. The Dundee goalkeeper Scott Bain then breathed a huge sigh of relief as his clearance ricocheted off the onrushing Jamie Walker and bounced narrowly wide of goal.
Captain Darren O'Dea headed Kevin Holt's corner over the crossbar in the hosts' first chance of the game after 15 minutes. Lewis Spence shot narrowly wide soon after, although clear scoring opportunities were at a premium for both teams throughout the first half. Just a minute before the interval, Dundee forced an opening and capitalised.
Holt's outswinging corner was glanced into the far corner of the net by young Waddell, who looked ecstatic scoring his first ever Dundee goal. It was the breakthrough McCann would have wanted at just the right juncture.
Hearts moved Buaben into midfield and changed to a back four at half-time. Striker Esmael Goncalves restarted in lively fashion with a determined run and shot which Bain got a hand to. From a similar position, the Portuguese striker then lashed a shot against the underside of the crossbar.
The away side were pushing but had to guard against counter-attacks. One such breakaway down the right saw Mark O'Hara's cross find Moussa, but the Hearts goalkeeper Jon McLaughlin was in place to gather the ball.
Hearts required a bit of fortune to help them restore parity and Moussa provided it on 70 minutes. Just inside his own half, he curiously chose to aim a lofted backpass towards goalkeeper Bain. Lafferty sensed an opportunity and sprinted on to the ball first. He touched it past Bain and rolled it into the unguarded net from an acute angle.
He made instantly for Levein and MacPhee in the technical area in a public show of appreciation for their recent support. Hearts weren't finished there, though. Walker Exchanged passes with Connor Randall on 76 minutes and would have given his team the lead but for Bain's fine save. The keeper stopped another impromptu effort from Walker moments later.
A dive to his right in stoppage-time allowed McLaughlin to push Dundee substitute A-Jay Leitch-Smith's shot for a corner. From the resultant cross by Paul McGowan, Waddell rose unchallenged to head the winner beyond a helpless McLaughlin.
Dundee (3-4-2-1): Bain; Waddell, Hendry, O'Dea; O'Hara, Kamara, Spence, Holt; Deacon, McGowan; Moussa.
65: Kerr for O'Dea
77: Leitch-Smith for Moussa
82: El Bakhtaoui for Deacon
Unused subs: Parish, Allan, Aurtenetxe, Wolters.
Hearts (3-4-1-2): McLaughlin; Souttar, Hughes, Berra; Randall, Callachan, Cochrane, M Smith; Walker; Lafferty, Goncalves.
7: Buaben for Hughes
73: Milinkovic for Cochrane
Unused subs: Hamilton, Martin, Stockton, Grzelak, Henderson.
Referee: Steven McLean.