A late Leon Goretzka goal salvaged a 2-2 draw for Germany against Hungary on Wednesday night, in a contest that set the Germans up for a June 29 last-16 match against England at Wembley.
The Germans were teetering on the brink of another shock tournament exit after Adam Szalai's diving header in the 11th minute put Hungary in front.
Kai Havertz eventually drew Germany level in the 66th minute, only for Hungary to retake the lead seconds after the restart as Andras Schaefer ran on to a pass and headed in.
Goretzka bagged the crucial equaliser in the 84th minute to send Germany through in second place behind France in Group F on four points.
"We made mistakes but we fought, showed character," said Germany coach Joachim Loew.
"It was not for the faint-hearted. At the end, to come through this group was good and that was the aim.
"What matters is that we are through. Now we play with England and we can look forward to that match.
"It is an absolute highlight to play at Wembley against England. We will be a different team than tonight, I can promise you that."
The run-up to the game was overshadowed by Uefa's refusal to allow the stadium to be lit in rainbow colours to oppose a Hungarian anti-LGBTQ law, and a protester waving a rainbow flag invaded the pitch before kick-off.
But it was all business for the hosts once the whistle was blown, as they were desperate to avoid a repeat of their 2018 World Cup debacle, with Joshua Kimmich testing keeper Peter Gulacsi after three minutes.
But it was the Hungarians who stunned the home crowd with Szalai's diving header.
The Germans had close to 70 per cent possession in the first half and Mats Hummels hit the crossbar with a powerful header.
But other than that they showed little of the attacking spark they demonstrated in their 4-2 win over Portugal.
Germany, who had never gone out at the group stage of consecutive major tournaments, showed nerves as their game gradually became more and more scrappy.
It was also a scrappy goal that brought them level, with Gulacsi failing to connect with a Kimmich free kick, Hummels heading the ball on and Havertz nodding in.
But as the German fans celebrated in the stands, Hungary scored straight after the restart, with Szalai lofting the ball into the German half and Schaefer lunging forward and heading it past keeper Manuel Neuer.
With Hungary defending desperately to protect their spot in the knock-out stages, Toni Kroos tried his luck from a tight angle but his effort flashed wide.
The German pressure finally paid off in the 84th when substitute Jamal Musiala cut the ball in the box and fellow substitute Goretzka drilled it home.
For Hungary, it was a bitter end to the tournament.
"It is difficult to find words," Gulacsi said. "We played a good game. Germany dominated but we scored two goals.
"We were unlucky. This is very bitter."