Joachim Low’s aspect trailed 2-1 and seemed to be heading for his or her second group defeat, which might have left them third within the desk, when substitute Goretzka fired them degree within the 84th minute.
Adam Szalai’s early header had given Hungary a flying begin and straight after Kai Havertz’s second-half equaliser for Germany, Andras Schafer headed the underdogs again in entrance.
Germany sealed second spot and will now play England within the spherical of 16 at Wembley on Tuesday, whereas Group F winners France, who drew 2-2 in opposition to Portugal, face Switzerland on Monday.
After the sport received underneath manner, Joshua Kimmich examined Hungary goalkeeper Peter Gulacsi with an early angled shot earlier than Germany fell behind within the eleventh minute.
Roland Sallai’s good, arcing cross from deep bisected Germany defenders Kimmich and Mats Hummels and Szalai stooped to go past stationary goalkeeper Manuel Neuer.
Germany responded via Hummels, who headed Kimmich’s cross following a nook in opposition to the crossbar.
Matthias Ginter was subsequent to cross up a gilt-edged likelihood for the Germans as he didn’t get full connection on a free ball in entrance of objective.
Germany hogged first-half possession, however Hungary chased every part down as if their lives relied on it and Szalai threatened once more shortly earlier than the interval.
Low changed Manchester Metropolis’s Ilkay Gundogan with Goretzka in a bid so as to add some urgency to his aspect, however the boos rang out as Leroy Sane over-hit his nook shortly afterwards.
Szalai was inches away from a second for Hungary when his free-kick, after Sane had dealt with, struck the surface of Neuer’s right-hand submit.
Germany equalised within the 66th minute when Havertz headed residence from shut vary after Gulacsi had didn’t get anyplace close to a looping free-kick into the field and the ball had struck Hummels on the top.
However the Germans have been degree for lower than two minutes. Straight from the restart, Szalai helped on an extended ball excessive and Schafer beat Neuer to the bounce to go Hungary again into the lead.
Germany chased one other equaliser in a bid to grab a Wembley date in opposition to England from Hungary‘s grasp and Toni Kroos flashed a shot simply huge within the 81st minute.
With the clock ticking down, Germany’s substitutes got here to the rescue. Jamal Musiala, who had changed Robin Gosens two minutes earlier, fired a low cross into the field and after Timo Werner’s effort was blocked, Goretzka lashed residence the rebound.