Iago Aspas' goal in the extra time booked Spain's seat in the elimination rounds of the ongoing 21st edition of the FIFA World Cup after they ended their last group stage match against already-eliminated Morocco 2-2 in Kaliningrad on Monday.
Khalid Boutaib gifted Morocco a lead in the 14th minute before Isco restored parity for Spain five minutes later.
Later on, Youssef En-Nesyri gave Morocco the lead for the second time in the 81st minute.
While Morocco almost had a chance to create the biggest upset of the 2018 FIFA World Cup, Aspas scored in second half injury time to avoid an embarrassing defeat for Spain.
Spain went behind for the first time when Andres Iniesta and captain Sergio Ramos somehow allowed Boutaib to seize upon a loose ball and run clear to score Morocco's first goal at the finals since 1998.
Iniesta soon made amends skipping through the Morocco defence to set up Isco for a leveller.
More From This Section
When Morocco substitute Youssef En-Nesyri got up highest to thump home a brilliant header from a Faycal Fajr corner with nine minutes to go, Herve Renard's men looked set to claim a memorable victory.
But after another substitute, Iago Aspas, tucked in a superbly improvised finish and again levelled the game 2-2.
Spain thus finished the group stage with five points from three matches.
Disclaimer: No Business Standard Journalist was involved in creation of this content