A dramatic night of the FIFA World Cup saw Japan come back from a goal down to beat Spain 2-1 and qualify for the last 16 as Group E winners ahead of Spain on Thursday.
Germany beat Costa Rica 4-2, but the win didn't save them from elimination from the World Cup at the group stage for the second time in succession.
The evening looked to be going to script for the two European powerhouses, as Spain and Germany took the lead in their respective matches, with Alvaro Morata heading Spain ahead in the 11th minute, while Serge Gnabry gave Germany a lead against Costa Rica.
However, the Spanish failed to turn their 80 percent of ball possession into more clear-cut chances, while Germany squandered several options to increase their lead before halftime.
Ritsu Doan equalized for Japan with a left-footed shot in the 48th minute and then combined with fellow substitute Kaoru Mitoma to set up Ao Tanaka three minutes later to put Japan ahead.
That goal meant Germany were out of the tournament, and things got worse for the Germans when Yeltsin Tejeda leveled for Costa Rica in the 58th minute. An own goal from Manuel Neuer, who diverted Juan Vargas' header into his own net, then put Costa Rica ahead in the 70th minute.
That goal meant Costa Rica were going through and Spain were on the way out, but Kai Havertz made an instant impact, flicking home and equalizer three minutes later.
That goal saved Spain, but not Germany. Although Havertz and Niclas Fullkrug added further goals to give Germany a 4-2 win, it was the hollowest of victories.
There was also tension as Morocco and Croatia qualified from Group F, with Morocco going through as group winners after they won an entertaining game 2-1 against Canada.
Hakim Ziyech accepted a gift from the Canadian defense to chip the ball into an empty net from around 30 meters after a horrible defensive mix-up.
The Moroccans soon doubled their lead in the 23rd minute thanks to striker Youssef En Nesyri, who held off his marker and scored a powerful strike on the run.
It looked to be game over, but the Canadians, who were already out of the tournament, showed spirit and a slice of luck when Nayef Aguerd deflected a shot into his own net.
The second half saw the Canadians go all out for an equalizing goal, and veteran striker Junior Hoilett was desperately unlucky to see his header hit the underside of the bar and bounce clear.
Morocco's win meant that either Croatia or Belgium would be going home, and a 0-0 draw meant it was the Belgians, despite Roberto Martinez's men playing their best football of the tournament.
Romelu Lukaku came on as a halftime substitute for Belgium, but missed several chances in the closing minutes, seeing one shot hit the post on the hour.
He was then unable to control a header, which went just over the bar, before flicking another chance wide with four minutes left to play.
His worst miss came in injury time when Thorgan Hazard's cross evaded everyone, but with the goal at his mercy, the ball hit Lukaku's chest and rolled tamely into the hands of the keeper.
After the match, Martinez announced he would not continue as Belgium coach.