A Kai Havertz double led Germany to a 4-2 victory over Costa Rica on Thursday, but Spain's loss to Japan in the other Group E match condemned the four-time champions to a second-straight World Cup exit before the round of 16.
Germany finished third in the standings, behind Spain on goal difference, with Japan top following their 2-1 win over the La Roja. The result left Japan facing Croatia in the last 16 while Spain will face Morocco.
On a night of unrelenting drama in the desert near Doha, Serge Gnabry scored an early goal to give Germany a 1-0 lead over Costa Rica at half-time, but despite dominating they were unable to add to their lead over the Ticos in the opening 45 minutes.