France won Group F at Euro 2020 following a 2-2 draw with defending champions Portugal. Germany, who twice came from behind against Hungary to also draw 2-2, advanced to the last 16 as runners-up.
Portugal ended the group stages with four points to progress as one of the four best third-placed teams and will take on Belgium on Sunday in Copenhagen.
France went into the game in Budapest already qualified for the knockout stages.
Portugal needed all three points to be certain of advancing.
But for about 20 minutes, while Hungary led Germany and France were in front, Fernando Santos' men were out of the tournament.
But Germany eventually equalised in Munich a second time through Leon Goretzka to avoid a shock defeat and a second consecutive exit after the group stages at a major tournament.
Hungary opened the scoring in Munich after 11 minutes. Kai Havertz nodded in the equaliser mid-way through the second-half following a blunder from the Hungary goalkeeper Peter Gulacsi. It was the Chelsea striker's second goal of the tournament.
But before Germany could savour parity, Hungary regained the lead from the restart.
Szalai lofted a ball over the German defence and Andras Schäfer headed the ball past the Germany goalkeeper Manuel Neuer.
Goretzka's strike means Germany will play the Group D winners England on 29 June at Wembley.
In Group E, Sweden won a thrilling match against Poland 3-2 to claim the pool and misfiring Spain came good. Luis Enrique's men thrashed Slovakia 5-0 to set up a last-16 clash with Croatia in Copenhagen on Sunday.