DOHA, Qatar (AP) - Same stadium. Same result. Similar shocking victory for Japan at the World Cup.

After beating Germany in the team's opener, Japan worked its way into the round of 16 of the World Cup on Thursday by defeating Spain 2-1 - the same score as last week.

Spain also advanced despite the loss, knocking Germany out of the tournament.

Japan scored twice early in the second half to come from behind and defeat another European powerhouse and make it to the knockout stage for the second straight World Cup. It's the first time Japan has advanced past the group stage in back-to-back tournaments.

“For Asia and for Japan, our victories over Spain and Germany, two of the top teams in the world, is something that give us great confidence and we are very pleased,” Japan coach Hajime Moriyasu said. “Of course, there are many things we still have to learn, but Asia can win in the world stage, Japan can win in the world stage.”

Ao Tanaka scored the winning goal from close range early in the second half. It took about two minutes for video review officials to confirm the ball hadn't gone out of bounds before the goal at Khalifa International Stadium, where Japan had shocked Germany in its opener.

Players from both teams looked surprised when it was announced that the goal counted, and the Japanese started running toward their bench again to celebrate with their teammates.

“From my angle I think the ball was clearly half out, but more than that I could not see because of the speed,” Tanaka said. “I was concentrating in scoring. There was always a possibility that it was out ... But in the end it was a goal, so that was great.”

Japan finished at the top of Group E and will next face Croatia. Spain will take on Group F winner Morocco.

Spain and Germany - which beat Costa Rica 4-2 in a simultaneous match - both had four points in the group. But Spain had a superior goal difference.

The group standings bounced back and forth during both matches. Costa Rica was beating Germany at one point. If that result had stood, Costa Rica would have eliminated Spain.

Spain coach Luis Enrique said he didn't know that at one point Costa Rica was winning.

“If I had known about it, I could have had a heart attack,” he said.

Alvaro Morata scored for Spain in the 11th minute and the 2010 champions were in control until Japan rallied after halftime. Ritsu Doan equalized in the 48th with a left-footed shot from outside the box and Tanaka added the second three minutes later.

Luis Enrique said Spain “got into collapse mode.”

“We totally lost control during those five minutes. We panicked,” Luis Enrique said. “If they had to score more goals, they would have been able to do it. They got past us like an airplane.”

Morata became the first Spain player to score in the team's first three matches at the World Cup since Telmo Zarra in 1950.

Spain had opened with a 7-0 rout of Costa Rica before conceding late in a 1-1 draw against Germany.

Japan, which lost to Costa Rica in its second match, was eliminated by Belgium in the last 16 four years ago in Russia. The Japanese have never gotten past the round of 16 at the World Cup.

By finishing second in the group, Spain may avoid facing Brazil in the quarterfinals and Argentina in the semifinals.

“There is nothing to celebrate,” Luis Enrique said. “I'm not happy. We wanted to finish in first place with a victory.”

The coach said he was surprised that the second goal by Japan was confirmed by VAR. He said he was shown a photo of the ball out of bounds.

“That photo they showed me must have been altered,” Luis Enrique said. “That's the only explanation.”