Leon Goretzka's late equaliser clinched Germany a draw against Hungary and a second-placed Group F finish on a rollercoaster night in Munich.
Joachim Low’s side trailed 2-1 and appeared to be heading for their second group defeat, which would have left them third in the table, when substitute Goretzka fired them level in the 84th minute.
Adam Szalai’s early header had given Hungary a flying start and straight after Kai Havertz’s second-half equaliser for Germany, Andras Schafer headed the underdogs back in front.
Germany sealed second spot and will now play England in the round of 16 at Wembley on Tuesday, while Group F winners France, who drew 2-2 against Portugal, face Switzerland on Monday.
The game kicked off in pouring rain and following a controversial moment when a fan ran onto the pitch holding a rainbow flag in support of the LGBTQI+ community during Hungary's national anthem.
After the game got under way, Joshua Kimmich tested Hungary goalkeeper Peter Gulacsi with an early angled shot before Germany fell behind in the 11th minute.
Roland Sallai’s brilliant, arcing cross from deep bisected Germany defenders Kimmich and Mats Hummels and Szalai stooped to head beyond stationary goalkeeper Manuel Neuer.
Germany responded through Hummels, who headed Kimmich’s cross following a corner against the crossbar.
We need your consent to load this comcast-player contentWe use comcast-player to manage extra content that can set cookies on your device and collect data about your activity. Please review their details and accept them to load the content.Manage Preferences
Matthias Ginter was next to pass up a gilt-edged chance for the Germans as he failed to get full connection on a loose ball in front of goal.
Germany hogged first-half possession, but Hungary chased everything down as if their lives depended on it and Szalai threatened again shortly before the interval.
Low replaced Manchester City’s Ilkay Gundogan with Goretzka in a bid to add some urgency to his side, but the boos rang out as Leroy Sane overhit his corner shortly afterwards.
Szalai was inches away from a second for Hungary when his free-kick after Sane had handled struck the outside of Neuer’s righthand post.
Germany equalised in the 66th minute when Havertz headed home from close range after Gulacsi had failed to get anywhere near a looping free-kick into the box and the ball had struck Hummels on the head.
But the Germans were level for less than two minutes. Straight from the restart, Szalai helped on a long ball over the top and Schafer beat Neuer to the bounce to head Hungary back into the lead.
Germany chased another equaliser in a bid to snatch a Wembley date against England from Hungary’s grasp and Toni Kroos flashed a shot just wide in the 81st minute.
With the clock ticking down, Germany’s substitutes came to the rescue.
Jamal Musiala, who had replaced Robin Gosens two minutes earlier, fired a low cross into the box and after Timo Werner’s effort was blocked, Goretzka lashed home the rebound.