Leon Goretzka's late equaliser was enough for Germany to draw 2-2 with Hungary and secure second spot in Group F, setting up a last-16 showdown with England.
Germany were hoping to follow up their 4-2 win against Portugal on Saturday with a comfortable victory against Group F underdogs Hungary, but made the worst possible start as Adam Szalai (11) nodded home.
The hosts were mostly laboured in their play, but were handed a lifeline as Mats Hummels and Kai Havertz took advantage of an error from goalkeeper Peter Gulacsi, allowing the Chelsea forward (66) to nod home from close range.
- How the teams lined up | In-depth stats and analysis
- Final Group F standings | Euro 2020 fixtures and schedule | Euro 2020 gossip column
- Joachim Low defends Germany form ahead of England meeting
But 91 seconds later and straight from the restart, Hungary re-took their precious lead. Szalai set up Andras Schafer (68) to slot past Manuel Neuer, sending Germany to the bottom of Group F and on the verge of a second successive exit in the group stages of a major tournament.
There was more drama to come though as Goretzka fired home in the 84th minute to fire Germany into second place, setting up an enticing last-16 tie with old foes England at Wembley on Tuesday.
It was heartbreak for Hungary, who were minutes away from reaching the knockout rounds for a second European Championships. It echoed their 3-3 draw against Portugal from Euro 2016 too, where they were pegged back three times during a thrilling encounter. However, in a tough group, they are able to exit Euro 2020 with their heads held high.
How a dramatic night in Munich unfolded...
Both sides had snapshot efforts in the opening ten minutes. Gulacsi made a good, reactive save as Joshua Kimmich fired his way from a tight angle, before Neuer came out to collect a good pass from Attila Szalai.
But Neuer found himself beaten soon after. It was a classic counter-attack from the Hungarians, with Roland Sallai and Loic Nego linking up superbly. It was Sallai who sent in a superb, diagonal cross into the middle and Hungary captain Adam Szalai was there to flick a header home, sending the ball past Neuer's fingertips.
Germany: Neuer (6), Ginter (7), Hummels (7), Rudiger (7), Kimmich (7), Gundogan (6), Kroos (6), Gosens (7), Havertz (7), Sane (5), Gnabry (5).
Subs used: Goretzka (6), Muller (6), Werner (6), Volland (5), Musiala (6).
Hungary: Gulacsi (6), Nego (7), Botka (7), Orban (7), Attila Szalai (7), Fiola (7), Kleinheisler (6), Nagy (6), Schafer (7), Adam Szalai (8), Sallai (7).
Subs used: Schon (5), Varga (5), Nikolic (n/a), Viera (n/a).
Man of the match: Adam Szalai.
Germany then had a few chances to equalise. Soon after the goal, Havertz made a superb run between Adam Nagy and Attila Szalai, but the slightest of touches from Gulacsi sent the ball away from the paths of Leroy Sane and Robin Gosens.
- Germany made one change from their win against Portugal - Leroy Sane made his first Euro 2020 start, replacing the injured Thomas Muller.
- Hungary named the same XI that drew 1-1 with France.
Germany's best effort of the first half came in the 21st minute. Kimmich delivered their first corner of the game, but Hummels could only direct his powerful header onto the crossbar. Soon after, Matthias Ginter poked the ball goalwards, but it was straight at Gulacsi.
Although Sallai hit the crossbar early in the second half, it was Germany who hauled themselves level in the 66th minute. Kimmich swung in a free-kick, which Gulacsi came out to punch away, but missed the ball completely. Hummels was lurking just behind to nod it goalwards, with Havertz helping to guide it home from close range.
But the 1-1 scoreline lasted for all of 91 seconds. Hungary pumped the ball forward, with Adam Szalai there to flick the ball on for Schafer's run in behind. It was a sweeping finish from the forward, to once again send Germany to the bottom of Group F and set up a tense, nervous finish.
Germany continued to push for an equaliser, opting for crosses into the box to target their taller players. But it was 18-year-old Jamal Musiala who created the much-needed leveller mere minutes after coming on.
He twisted past Endre Botka on the left, before cutting the ball back to Goretzka. He laid it off for Timo Werner, but the Chelsea forward's shot was deflected back into Goretzka's path. He then powered a volley goalwards, securing Germany's spot in the last-16 in the process
Man of the match - Adam Szalai
It was an impressive night's work from Hungary captain Adam Szalai. Hungary had little pressure on them, but his opening goal was taken superbly. He then unselfishly set up his team-mate for the second as Hungary dared to dream of the knockout round.
While Hungary did not quite make it through, Szalai was still making the tackle and passes needed, even late in the day. A fine performance to finish Euro 2020.
What the managers said
Germany coach Joachim Low: "We showed great morale, but we also made mistakes. It was good to see that we fought hard until we got the equaliser. It was a nervy match. We knew Hungary would give it their all. I am just happy we got through in this group.
"All the 'small' teams invest so much work and defend well. Hungary played with ten men in their own half and they had nothing to lose. It's great that we got through in this tough group. Now we can look forward to a match against England."
Hungary coach Marco Rossi: "We went very close to a qualification which would have not just been historic, it would have been incredible and almost unimaginable. But unfortunately even in the greatest fairy tales, there can be some less than happy endings, and that was the way it went tonight.
"That said, I am very proud of my players. I know from a technical perspective we are not one of the best teams in the world, but from every other perspective we are. Tactical, how we help each other out, we dig in and show sacrifice, I am very proud to coach this side. We are disappointed, we went very close, but I congratulated all of the team and told them to keep their heads up, because two months from now World Cup qualification resumes.
"I would challenge anyone to say that coming up against these teams to say we could get any points at all. But we got two points; we drew against France and here against Germany. I think these will go down in Hungarian recent footballing history."
- Germany are one of only four sides to concede first in all three of their group stage games at EURO 2020, after Turkey, North Macedonia and Poland.
- Aged 22 years and 12 days, Kai Havertz became the fourth-youngest player to score in back-to-back major tournament appearances for Germany (World Cup/Euros), after Thomas Müller (20-years-old in 2010), Franz Beckenbauer (20 in 1966) and Lukas Podolski (21 in 2006).
- Opening the scoring within 11 minutes, Hungary's Adam Szalai netted what was the quickest goal that Germany have conceded in the European Championships since 2000 v Romania (five minutes).
- Aged 18 years and 117 days, Jamal Musiala became the youngest ever player to make an appearance for the German national team at a major tournament (World Cup/Euros).
Germany will now travel to London on Tuesday to face England in the last-16; kick-off 5pm. Hungary will next play in September in the World Cup Qualifiers, facing England on Thursday September 2.
Win £300,000 with Super 6!
The Super 6 jackpot has rolled over. Could you land the £300,000 on Tuesday? Play for free, entries by 8pm.