Japan pulled off another historic World Cup upset as a controversial goal saw them come from behind to beat Spain and claim top spot in Group E.
Spain dominated the first half at the Khalifa International Stadium and looked like they were going to advance in first place after Morata headed home his third goal of the tournament.
But it all changed after half-time as substitute Ritsu Doan equalized with a shot from the edge of the box, and Ao Tanaka completed the comeback from a Kaoru Mitoma cut-back which appeared to have gone out of play.
A lengthy VAR check followed the goal, but it sensationally ruled in favor of Japan, the ball not being deemed to have crossed the line, leaving Spain to sweat on their place in the last 16.
At one point, when Costa Rica went in front in their 4-2 loss to Germany, Spain were heading out along with Hansi Flick's side, but in the end their superior goal difference enabled them to sneak through to the knockout stages.
Japan, who came from behind to beat Germany in the group stage, will face Croatia in the next round.
The first period was all Spain, with Morata's towering head from Cesar Azpilicueta's cross putting them in complete control.
The one- goal margin looked sufficient given Spain's complete dominance of possession and the fact that Morata had other opportunities.
The game against Germany was turned on its head by Japan boss Hajime Moriyasu who used his bench to great effect.
After a poor pass from goalkeeper Unai Simon caused Spain to lose possession deep in their own territory, Doan unleashed a powerful effort from the edge of the box which Simon was unable to stop.
A controversial contribution was made by another substitute as he cut the ball back from a cross at the byline for Tanaka to score.
The on-field officials initially ruled the goal out, deeming the ball to have gone out, and despite initial replays appearing to back them up, the VAR ruled that it should count.
Huge drama unfolded in the other game between Germany and Costa Rica as Spain looked shell-shocked.
At one point, Spain appeared to be going out, but Germany's recovery ensured they remained in second place with a goal difference of six.
Japan finished the game with just 18 per cent of the possession, the lowest share by any winning team at a World Cup since 1966, but the victory sent them through to the knockout stages for a second consecutive finals.
Gary wondered why television viewers weren't shown all the angles of the winning goal.
He said that the high cam on the line suggested that some of the ball may have crossed the line.
I've struggled with it a little bit because we've not been given the correct angles.
We don't see the VAR cameras in the premier league.
"If they don't show a picture that shows clearly that it didn't go out of play, you're thinking something isn't right," Souness said.
There are 80 million Germans who are waiting for a picture to show that the ball didn't hit the wall.
Germany is a big football nation. Why don't you clear it up right away?
Why don't we see something that's so controversial?
They are not showing it to us. Please clear it up.
"We are classified for the round of 16 and I am not happy at all."
We were the ones who lost after the match in which the rival was dominated for 80% of the time. I don't celebrate victories.
We are qualified, but I would have liked to be at the top of the group.
They scored two goals in five minutes and dismantled us in the next 10 minutes.
We have entered a state of collapse. They would have scored against us if they needed to.
I have faith in my team. I don't think panic will change what I think. My job is to make people feel better.
I would have had a heart attack if I'd known that Costa Rica was leading Germany.
"We played against Spain, one of the best teams in the world, and we knew before the game that it was going to be very tough and difficult."
The players did well despite the fact that they conceded a goal.
There were many fans who traveled all the way from Japan. We are giving this victory to the people of Japan.
Group E winner Japan will play Group F runner-up Croata in the last 16 on Monday at 3pm.
Spain and Germany finished second and third, respectively, and will face off in the last 16 on Tuesday at 3pm.
Costa Rica finished bottom of the group with three points, the same as Germany.