Four-time champions Germany have sensationally crashed out of the World Cup despite a 4-2 victory over Costa Rica in their last Group E match, tumbling out at the first hurdle for the second consecutive time.
Germany finished third in the standings on Thursday, behind Spain on goal difference, with Japan top following their 2-1 win over the Spaniards.
The result left Japan facing Croatia in the last 16 while Spain will take on Morocco.
On a night of unrelenting drama in the desert near Doha, the Germans struck first with Serge Gnabry in the 10th minute but saw the Central Americans score twice with Yeltsin Tejeda and a Manuel Neuer own goal before a Kai Havertz brace put them back in front.
Fellow substitute Niclas Fuellkrug added a fourth in stoppage time but it could do nothing to prevent their elimination as Spain advanced with a superior goal difference.
It was the first time in their illustrious World Cup history that Germany had failed to win any of their first two group matches and despite bagging a win on their last attempt, they will be departing Qatar with dropped heads.
“In the first half I was disappointed and very angry at my team and how we allowed the opponent to come back,” Germany coach Hansi Flick said.
“We wanted to score three or four goals in the first half but then we made mistakes. If we had converted those chances, 16 of them. But the tournament was not decided today for us. We did not have any efficiency at this tournament and that is why we were eliminated”