Germany crashed out of the World Cup on a night of drama despite victory over Costa Rica at Al Bayt Stadium.
Japan’s controversial 2-1 victory against Spain would have eliminated Germany anyway, but Hansi Flick’s side needed three points to give themselves any chance of reaching the last 16.
They completed their part of the bargain, but it was to no avail as the four-time champions finished third in the group and were eliminated on goal difference.
It could have been a different story after Japan’s winning goal in the other Group E match was initially ruled out because the officials thought the ball had gone out of play before it was put into the net, but the decision was overturned by VAR.
Germany were in complete control and in front through Serge Gnabry’s 10th-minute header, only for Costa Rica to turn the match on its head to raise the remarkable possibility of Spain also going out.
Yeltsin Tejeda equalised after Germany keeper Manuel Neuer pushed out Kendall Waston’s header in the 58th minute, before Neuer was deemed to have the final touch for the second goal 12 minutes later when Juan Pablo Vargas bundled in from close range.
At that stage, Costa Rica were going through with Japan, and Spain were heading for the exit.
It would have capped one of the most astonishing nights in World Cup history, but Germany substitute Kai Havertz restored a measure of normality with two clinical finishes to extinguish Costa Rica’s hopes of springing a major shock.
Substitute Niclas Fullkrug added a last-minute fourth to confirm victory, but it will be of no consolation as Germany failed to reach the knockout stage of the tournament for the second time in succession.