Steven Craig's 92nd-minute goal sent Dundee into a Tennent's Scottish Cup semi-final meeting with Gretna at Hampden - and also spared the blushes of calamity goalkeeper Kelvin Jack.
The Trinidad and Tobago international allowed Juanjo's free-kick to elude him first and then bettered that error by missing a punch in the build-up to Pat Keogh's late equaliser.
Dundee had placed one foot into the next round with goals from Bobby Mann and Simon Lynch, before Accies' stirring comeback.
However, they regrouped for extra-time and Craig's stunning strike proved the match-winner, setting up a money-spinning trip to the national stadium.
The Dark Blues enjoyed the better of a hard-fought first-half, but Hamilton had the best chance when Mark Gilhaney created an opportunity for Scott Tunbridge six yards out but the striker could not connect.
Dundee teenager Bryan Deasley, who has been likened to former Dundee forward Nacho Novo, demonstrated devastating pace and an awareness which belies his 17 years.
He skipped past two defenders in the 36th minute, but, as he finally let fly Mark McLaughlin bravely threw himself headlong to block.
Deasley was causing mayhem for the visitors' defence and Tom McManus should have done better with a chance the teenager laid on a plate for him five minutes into the second half.
Former Scotland Under-21 striker McManus gathered the ball on the right-hand side of the penalty area but leaned back too much and shot over the gaping net.
Paul Dixon's massive throw-ins had been a constant feature of Dundee's play and they finally paid dividends in the 57th minute.
The youngster, who was watched by Everton earlier in the season, planted a 35-yard throw-in square on Mann's head for the stand-in captain to steer a low effort beyond McEwan.
Dixon switched flanks to take his throws and almost created another goal in the 66th minute. This time Wilkie connected but McEwan dived low to steer the ball away from his bottom corner.
Dundee then added a second which at the time seemed ready to sink Accies.
Dens Park top-scorer Simon Lynch snuck in ahead of the visitors' defence to flash a shot beyond the despairing goalkeeper and score another goal of stunning simplicity.
That should have set Dundee on the home straight to Hampden but they capitulated in stunning fashion courtesy of Jack.
If Wayne Rooney or any of his England colleagues had been watching this televised match they were surely rubbing their hands with glee ahead of this summer's World Cup.
First substitute Juanjo ensured that the game would be alive going into the final 14 minutes, scoring straight from a free-kick. The Spaniard's curling cross eluded the entire home defence before swirling past Jack.
They grabbed the lifeline in the 82nd minute as Jack's nightmare evening took a further turn for the worse.
The goalkeeper came to punch a free-kick but missed, causing a scramble in the box which concluded with substitute Pat Keogh chesting the ball over the line to complete a remarkable comeback.
Steven Craig grabbed the eventual winner after only two minutes of extra-time.
The substitute was fortunate to get the break of the ball on the edge of the area but followed it up with a sweet shot which rifled into the roof of McEwan's net to send the Dark Blues to Hampden.
Teams:
Dundee Jack, McCluskey, Mann, Wilkie, Dixon, Robertson,McManus (Ferguson 98), Kevin McDonald, Brady,Deasley (Craig 76), Lynch (Calum McDonald 116).
Subs Not Used: Murray, Law.
Booked: Kevin McDonald, McManus.
Goals: Mann 56, Lynch 68, Craig 91.
Hamilton McEwan, Balmer, Thomson (Wilson 70), McLaughlin,Ferguson (Juanjo 51), Robertson, McArthur, Neil,Tunbridge (Keogh 70), Wake, Gilhaney.
Subs Not Used: Jellema, Elebert.
Booked: Thomson, Balmer.
Goals: Juanjo 75, Keogh 82.
Att: 7,460
Ref: D McDonald (Scotland).