Morocco almost created the biggest upset of the 2018 FIFA World Cup before a late goal handed Spain a 2-2 draw in a Group B match here on Monday.
Khalid Boutaib gifted Morocco a surprise lead in the 14th minute before Isco (19th) restored parity for Spain five minutes later.
Youssef En-Nesyri gave Morocco the lead for the second time in the 81st minute.
But Iago Aspas scored in second-half injury time to avoid an embarrassing defeat for Spain.
Spain thus finished the group stage with five points from three matches and are through to the second round.
With Iran stunning heavyweights Portugal in the day's other Group B game by holding the reigning European champions to a draw, Spain finished at the top of the group.
Although both Spain and Portugal finished the group stage with five points each, the former have a better goal difference.
Spain will meet hosts Russia in the next round. Russia had finished second in Group A.
Needing a draw to avoid a first-round exit, star-studded Spain struggled against the well organised Morocco defence throughout the game.
The only aspect in which Morocco was stronger than Spain were the aerial balls, thanks to their height and physical strength.
The North Africans used that advantage to full effect.
While Spain kept the Morocco defence under tremendous pressure, for the most part, most of the crosses and long balls were dealt with effectively by the Africans.
At the other end of the pitch, Morocco repeatedly troubled the Spanish defence with long balls.
Spain central defenders Pique and Sergio Ramos had to fight a tough, physical battle to keep the Morocco forwards at bay.
Morocco created the whiff of a possible upset when a mix up between Iniesta and Sergio Ramos allowed Boutaib to snatch the ball and race clear on goal before beating David de Gea in the Spanish goal with a calm finish.
Stunned by the goal, Spain went all out for the equaliser.
They did not have to wait long as a superb build-up saw Iniesta storm into the Morocco penalty box.
Almost through on goal, the veteran midfielder caught his marker and Morocco goalkeeper Munir Mohamedi off guard with a feint and found Isco with a pullback.
With Mohamedi caught off balance, the unmarked Isco had a problem in finding the net.
Morocco were unlucky not to get a second goal in the 56th minute when Amrabat'slong-rangee shot hit the underside of the crossbar and bounced out with De Gea well beaten.
Isco almost got a second goal in the 62nd minute but Morocco defender Romain Saiss cleared his header off the goal line.
Off the subsequent corner, Pique 's attempt thundered narrowly past the post.
The match was set up for a dramatic last few minutes when Youssef rose above the Spain defence to nod in Faycal Fajr's cross off a corner.
That left Spain starting at the frightening prospect of an embarrassing defeat and a possible first round exit.
But Aspas ensured a passage to the second round for the former World Cup champions when he met Isco's cross with a beautiful finish.
The goal was initially ruled offside, but VAR technology came to Spain's rescue by overturning the referee's decision.
That prompted some heated exchanges on the sidelines and on the pitch as the entire Morocco squad and coaching staff exploded in protest.
But it was all to no avail as Spain saw off the remaining couple of minutes of added time and saved themselves by the skin of their teeth.