Despite a 4-2 win over Costa Rica in their last Group E match on Thursday, four-time champions Germany were knocked out at the first hurdle for the second time in a row.
Germany finished third in the standings, behind Spain on goal difference, with Japan leading after their 2-1 win over the Spaniards.
The result left Japan facing Croatia in the round of 16, while Spain will face Morocco.
In a night of unrelenting drama in the desert near Doha, the Germans scored first through Serge Gnabry on 10 minutes, but the Central Americans scored twice from Yeltsin Tejeda and a Manuel Neuer own goal before a brace from Kai Havertz put them out. back to the front.
Fellow substitute Niclas Fuellkrug added a fourth in stoppage time, but could do nothing to prevent their elimination as Spain advanced on superior goal difference.
It was the first time in their illustrious World Cup history that Germany had failed to win either of their first two group games and, despite picking up a win on their last attempt, they will leave Qatar with their heads down.
“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 didn’t have any efficiency in this tournament and that’s why we were eliminated.”
They were on the verge of elimination before the match after losing their opening group match to Japan and then earning a 1-1 draw against Spain.
Needing victory only to avoid a repeat of their shock first-round elimination at the 2018 World Cup with seven Bayern Munich players in their line-up, Germany charged early looking for an early goal to calm down. your nerves.
Jamal Musiala cut from the left into the box and tested goalkeeper Keylor Navas with a low shot in the second minute.
Initially the traffic was one way and the unmarked Thomas Mueller should have done much better when selected by Joshua Kimmich but was unable to keep his header on target.
Gnabry, however, scored from a deflected header in the 10th minute to put the four-time champions ahead.
Needing at least one point to have a chance to progress, Costa Rica rarely crossed into the German camp, holding just 25% possession in the first 45 minutes.
Germany were eager to make amends for their poor start to the tournament with the teenager Musiala repeatedly coming up against the entire Costa Rican defense.
The chances kept coming but the Germans, like in the match against Japan, struggled to convert them.
They were nearly punished when Keysher Fuller benefited from a defensive double error, but his shot was deflected over the crossbar by Neuer, whose 19th appearance in the tournament was a world record for goalkeepers.
With the group table appearing on the big screen at Al Bayt Stadium, Germany became nervous with Musiala hitting the post twice early in the second half.
Neuer, however, was helpless 13 minutes after the restart when Tejeda passed him the ball on the rebound having first saved a header from Kendall Waston.
Juan Pablo Vargas then appeared to have flicked the ball over the line in the 70th minute to put Costa Rica in front, but the effort was later credited as an own goal by Neuer.
Substitute Havertz scored twice in 12 minutes and Fuellkrug added another in stoppage time, but ultimately it was too little too late with Spain advancing on superior goal difference.