Germany will face England in the last 16 of Euro 2020 after twice coming from behind to knock battling Hungary out of the tournament.
An outstanding header by Adam Szalai, who plays his club football in Germany for Mainz, had given Hungary a shock lead in Munich.
Andras Schafer thought he had won it for the Hungarians with a header - 90 seconds after Kai Havertz had equalised - before substitute Leon Goretzka made it 2-2 six minutes from the end.
It means Germany finish second in Group F and will now meet Gareth Southgate's England at Wembley in a hugely anticipated last-16 match next Tuesday (17:00 BST).
Hungary suffer heartbreak
In an extraordinary match, Germany were minutes away from making an embarrassing early exit when midfielder Goretzka, on as a substitute, equalised in the 84th minute with a low finish from 16 yards to send Hungary out.
Few outside Hungary will have given Marco Rossi's side any chance of advancing after losing 3-0 to holders Portugal in their opening group game.
But they followed up a gutsy 1-1 draw against world champions France with a disciplined performance to give the home fans in the Allianz Arena a scare.
They opened the scoring after Roland Sallai's incredible long-range pass picked up his captain Szalai to beat Manuel Neuer with a diving header.
Chelsea's Havertz equalised with a header following an error by goalkeeper Peter Gulacsi but Germany fans were still celebrating when Schafer's brave header in front of Neuer, who had come rushing off his line, put Hungary back in front.
At that stage, Hungary were heading into the last 16 and Germany were set for an early exit before Bayern Munich's Goretzka came to the rescue to break Hungarian hearts - and set up a mouth-watering match against England.
That match will revive memories of when the two nations met in the semi-finals of Euro 96 - Germany winning a penalty shootout at Wembley to deny the Three Lions a place in the final.
Player of the match
10Gnabry1Neuer4GinterSubstituted forVollandat 82'minutes5Hummels2Rüdiger6Kimmich21GündoganBooked at 29minsSubstituted forGoretzkaat 58'minutes8Kroos20GosensSubstituted forMusialaat 82'minutes7HavertzSubstituted forWernerat 67'minutes19SanéBooked at 61mins10GnabrySubstituted forMüllerat 67'minutes
20Sallai9Szalai1Gulácsi7Nego21BotkaBooked at 28mins6Orban4Szalai5FiolaBooked at 66minsSubstituted forNikolicat 88'minutes15KleinheislerSubstituted forLovrencsicsat 89'minutes8Nagy13Schäfer20SallaiSubstituted forSchönat 75'minutes9SzalaiBooked at 64minsSubstituted forVargaat 82'minutes
Match ends, Germany 2, Hungary 2.
Second Half ends, Germany 2, Hungary 2.
Corner, Germany. Conceded by Ádám Nagy.
Corner, Germany. Conceded by Endre Botka.
Corner, Germany. Conceded by Ádám Nagy.
Jamal Musiala (Germany) wins a free kick on the left wing.
Foul by Endre Botka (Hungary).
Substitution, Hungary. Gergo Lovrencsics replaces László Kleinheisler.
Substitution, Hungary. Nemanja Nikolic replaces Attila Fiola.
Goal! Germany 2, Hungary 2. Leon Goretzka (Germany) right footed shot from the centre of the box to the bottom right corner.
Attempt blocked. Timo Werner (Germany) right footed shot from the centre of the box is blocked. Assisted by Leon Goretzka.
Substitution, Germany. Kevin Volland replaces Matthias Ginter.
Substitution, Germany. Jamal Musiala replaces Robin Gosens.
Substitution, Hungary. Kevin Varga replaces Ádám Szalai.
Attempt missed. Toni Kroos (Germany) right footed shot from the left side of the box misses to the right. Assisted by Robin Gosens.
Attempt missed. Thomas Müller (Germany) header from the centre of the box is just a bit too high. Assisted by Toni Kroos with a cross.
Substitution, Hungary. Szabolcs Schön replaces Roland Sallai.
Attempt saved. Ádám Szalai (Hungary) right footed shot from more than 35 yards is saved in the centre of the goal.
Attempt saved. Robin Gosens (Germany) header from the left side of the box is saved in the centre of the goal. Assisted by Joshua Kimmich.
Goal! Germany 1, Hungary 2. András Schäfer (Hungary) header from the centre of the box to the bottom right corner. Assisted by Ádám Szalai with a through ball.