Nicolas Fullkrug saved Germany with an injury-time equalizer that snatched first place in Group A, just when it looked like Switzerland was poised to pull off a monumental upset.
And a final twist also came in the other match in the group, with Hungary, which lost its first two games and was on the very verge of elimination, giving itself a lifeline when Kevin Csoboth scored in the 10th minute of injury-time to beat Scotland 1-0.
In Frankfurt, sub Fullkrug was the man for a crisis, launching his powerful header into the net two minutes into added time as the host nation seemed destined to be pushed into a potentially treacherous round of 16 clash.
A few minutes later, in Stuttgart, Csoboth capitalized on a late breakaway and supplied a cool finish, with Scotland exposed having thrown men forward in search of its own crucial goal.
Julian Nagelsmann's Germany went into its match having won its first two games and with a two-point lead at the top of the group, with qualification secure and needing only a draw to clinch first place.
However, the fluidity and energy present against Scotland and Hungary was missing here, as the lively Swiss worked tirelessly for their lead and even harder to hold onto it.
Dan Ndoye's struck after 29 minutes to temporarily turn Group A on its head. Remo Freuler's neat cross was met with perfect timing, and Ndoye smacked it high into Manuel Neuer's goal.
The significance of topping the group means that Germany will now play the second-place team in Group C, currently Denmark but possibly Slovenia or Serbia, instead of the runner-up in Group B.
That looks most likely to be defending champion Italy or World Cup bronze medalist Croatia, with those two teams meeting each other on Monday.
Germany tried to get back into contention throughout the second half, and on 49 minutes a brilliant through ball from Florian Wirtz found Jamal Musiala, who smashed it straight at Yann Sommer. The rebound fell to Ilkay Gundogan, but was traveling too quickly for him to control.
Earlier, Swiss goalkeeper Sommer was mightily relieved when VAR ruled out what looked like being Germany's opening goal after 17 minutes. Robert Andrich's strike from outside the area saw Sommer dive lethargically to his right, with the ball bouncing over his lunge and into the net.
However, replays showed Musiala had barged into Michel Aebischer in the build-up, negating the goal.
A late free-kick from Toni Kroos was punched out by Sommer and landed with Leroy Sane, who could only steer his shot high and wide.
Switzerland thought it had sealed it when Ruben Vargas produced a fine finish, but he was adjudged to be narrowly offside just as he wheeled away to celebrate after beating Neuer.
The final opportunity fell to Borussia Dortmund striker Fullkrug, who is coming off a fine season and brings physicality and scoring potential every time he come on. His aim, as per usual, was true.
The dramatic result will only add to the national optimism that has built up in Germany, with all the pre-tournament concerns after poor friendly results now forgotten.
For the Swiss, the disappointment of conceding late was offset a little by its secure place in the knockout phase, meaning it has now emerged from the group stage at a major tournament for the sixth consecutive time.
Hungary, meanwhile, sits on three points and faces a nervous wait to see if it qualifies as one of the top four third-place teams, while Scotland heads home.
Martin Rogers is a columnist for FOX Sports. Follow him on Twitter @MRogersFOX and subscribe to the daily newsletter.