Leon Goretzka came off the bench to rescue Germany's Euro 2020 campaign with a late equaliser as a nerve-shredding 2-2 draw against Hungary in Munich on Wednesday set up a last-16 showdown with England.
Adam Szalai gave Hungary a shock early lead and although Germany drew level through Kai Havertz in the second half, the Hungarians went back in front almost immediately through Andras Schaefer.
With six minutes left, Goretzka fired home a crucial equaliser to set up a blockbuster tie against England at Wembley on Tuesday, eliminating Hungary who finished bottom of Group F.
"I am absolutely delighted -- we have no doubts now and are full of confidence," said Goretzka amid German celebrations at the final whistle.
Germany only narrowly avoided a repeat of their shock group-stage exit, as defending champions, at the 2018 World Cup in Russia.
"We made mistakes but fought and showed superb morale," said Germany coach Joachim Loew, who was six minutes from ending his 15 years in charge with an embarassing defeat.
"It was not for the faint-hearted. Now it will a real highlight to play against England at Wembley.
"We will be well prepared -- I can promise that."
Hungary goalkeeper Peter Gulacsi admitted it was tough to accept bowing out after leading twice.
"It's hard to find the words," he said.
"We played well. Clearly Germany dominated. In the end, it's bad luck for us and good luck for Germany."
The match build-up had been dominated by UEFA's refusal to allow the city of Munich to light the Allianz Arena in rainbow colours in solidarity with Hungary's LGBTIQ community.
UEFA and the government of Hungary came under a hail of criticism after Budapest's new anti-LGBTIQ law and the football body's refusal to light the Munich stadium.
Germany got off to the worst possible start when Szalai ghosted in between centre-backs Mats Hummels and Matthias Ginter to head Roland Sallai's superb cross past Manuel Neuer.
Delighted travelling Hungarian fans celebrated wildly as their German counterparts looked on in silence.
Hungary taking the lead with just 11 minutes gone sparked Germany into action as Hummels hit the crossbar, then Ginter fired straight at visiting goalkeeper Peter Gulacsi.
A cloudburst over north Munich soaked the Allianz Arena pitch as Germany poured forward in a desperate search for an equaliser.
The euphoria felt across the country after Germany's spectacular 4-2 win over Portugal just four days previously had suddenly evaporated.
But the hosts drew level on 66 minutes when Gulacsi failed to catch a Joshua Kimmich free-kick, Hummels headed the ball on and Havertz nodded into an empty net.
The joy in the German ranks was short-lived as Hungary regained the lead straight from the restart.
A long ball found Szalai, who lofted a pass over the defence where Schaefer got between the scrambling German defenders to head past an onrushing Neuer.
The crucial second German equaliser was down to two substitutes.
Teenager Jamal Musiala's cutback found his Bayern Munich team-mate Goretzka, who set up Timo Werner for a blocked shot.
But the rebound dropped to Goretzka who smashed the ball into the back of the net to avoid humiliation for Germany.