Germany crashed out of the World Cup on a remarkable night of drama despite victory over Costa Rica at Al Bayt Stadium.
Hansi Flick’s side needed three points to give themselves any chance of reaching the last 16 and 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.
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 Juan Pablo Vargas bundled in a second 12 minutes later.
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 to proceedings 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.
Meanwhile, Japan staged another extraordinary World Cup comeback to reach the last 16, overcoming a stunned Spain side who were fortunate to join them in the knockout stage.
In a tournament that has been full of shocks, this was perhaps the most dramatic night so far, with Spain left relying on the result of Germany’s game with Costa Rica for them to progress
All four teams had started the evening with a chance of progressing but with Spain needing only a draw to qualify, they were not expected to need any favours from elsewhere.
La Roja were cruising at the break after Alvaro Morata headed home Cesar Azpilicueta’s cross from close range, but Japan would quickly turn the game – and the whole of Group E – on its head.
The Samurai Blue sent on substitutes Ritsu Doan and Kaoru Mitoma at half-time and within six minutes of the restart, they had both made a huge impact.
Doan equalised with a fierce shot from the edge of the area and, only 142 seconds later, Mitomo retrieved Doan’s low cross and found Ao Tanaka to bundle home from close range
The goal was initially ruled out by the assistant referee who signalled Doan’s cross had gone out of play, but the Video Assistant Referee (VAR) ruled Mitomo had reached it in time.
Things got even worse for Spain when Costa Rica took a surprise lead over Germany moments later, at that stage they were heading out.
The big screens at Khalifa International Stadium showed the updated group table, to the disbelief of the Spain supporters, and pretty much everyone else here too.
Luis Enrique’s side were in big trouble, and were showing few signs of knowing how to rescue themselves, with only Germany’s late rally sending La Roja through as group runners-up on goal difference.