Germany thought they had grabbed the lead in the 17th minute when Robert Andrich fired a spinning shot over Yann Sommer, but VAR intervened owing to a foul by Jamal Musiala.
Switzerland took the lead moments later when Dan Ndoye hoisted the ball high into the roof of the net following Remo Freuler’s cross.
Switzerland’s Ruben Vargas scored again late on, but the flag was raised for offside before Havertz struck the crossbar at the opposite end.
Then, in the second minute of extra time, Germany’s perseverance paid off, with substitute Niclas Fullkrug headed back across goal from David Raum’s cross for 1-1.
As a result, Germany finished first in Group A, with Switzerland second. Scotland was eliminated following a late 1-0 defeat to Hungary, which can still advance to the knockout stages as one of the four best third-placed teams.
Germany’s round of 16 match will be played on Saturday, June 29 at 8 p.m. BST against the Group C runners-up, which may be England, Denmark, Slovenia, or Serbia.