Japan rallied from a goal down to shock European giants Spain 2-1 in the concluding match of Group E to emerge as group winners while booking a place in the knockouts of the FIFA World Cup.
In material of losing the last league match of the group Spain qualified along with Japan to the knockout stage of the tournament.
In a night of chaotic pair of games, at the Khalifa International Stadium, Japan trailing by a 11th minute goal scored by Alvaro Morata to go into the half time break trailing 1-0.
The Samurai Blue however stormed back with full gusto in the second half to score two goals within a span of three minutes.
Coach Hajime Moriyasu drew level through Ritsu Doan in the 48th minute and went into the lead with a goal from Ao Tanaka three minutes later.
Japan, who have set a target of qualifying for the last-eight stage of the tournament, had opened their campaign with a 2-1 win over four-time winners Germany but lost to Costa Rica.
Spain on the other hand started with 7-0 drubbing of Costa Rica and followed it up with a one-all draw with Germany.
In the other match at Al Bayt Stadium in Al Khor, Germany defeat Costa Rica 4-2 in a six-goal thriller which was officiated by a first all-female on-field refereeing team at men's World Cup.
The winning effort fell short and was not enough as they finished with just four points from three matches the same as Spain but La Roja qualified on a better goal difference.
Japan's 2-1 win over Spain meant Japan and Spain were through, Japan finished with six points from two wins and loss.
This is the second tournament in a row, Germany go out in the group stages. It's one of those nights where it just hasn't been enough.
Germany's failure to advance for the second straight World Cup is a remarkable fall for a national team that won the event in 2014, finished third in 2010 and 2006 and was the runner-up in 2002.
On Thursday, both Group E and Group F had surprising group winners. Group E champion Japan will face Croatia in the round of 16 on Monday. Group F winner Morocco will face Spain on Tuesday.
The World Cup continues Friday, when the round of 16 will be set after the finales in Group G and Group H.
Moriyassu's last-eight target, set at the start of the tournament, is now a step away.
Moriyasu has had some bitter memories of Qatar, as a player as he was part of the Japan team which missed out on the chance to qualify for the 1994 World Cup Finals as they were within seconds of reaching their first finals only to concede a stoppage-time equalizer to Iraq which sent arch-rivals South Korea through instead.
That crushing moment is known as the "Agony of Doha" but while Japan's biggest football setback may never be forgotten, the Samurai Blue are determined to reverse their fortunes and make Qatar the stage for their best World Cup performance.
Japan have since then put the 1994 set behind them and since debuting in 1998 have qualified for seven successive World Cup tournaments and a last-eight finish is finally within Japan's reach despite featuring in a tough group feels Moriyasu, which he has reiterated time to time ahead of their opening match.