DOHA: Achraf Hakimi struck the decisive penalty in a shootout to send Morocco into the World Cup quarter-finals for the first time on Tuesday (Dec 6) as the African side stunned Spain after a goalless draw.
Spain coach Luis Enrique had claimed he set his players the "homework" of taking 1,000 penalties each.
But Pablo Sarabia, Carlos Soler and Sergio Busquets all missed from the spot before Madrid-born Hakimi dinked his home as Morocco won the shootout 3-0, to the delight of their raucous support.
Walid Regragui's side will face Portugal in what is uncharted territory for them - their previous best run at a World Cup was a last-16 exit in 1986.
Spain had been hoping they could repeat their 2010 World Cup triumph after reaching the semi-finals of Euro 2020, where they were also beaten on penalties by Italy.
Sevilla goalkeeper Yassine Bounou had other ideas and saved brilliantly from Soler and Busquets, while Sarabia hit the post from the spot.
"The penalties cost us but I am proud of my team," said Luis Enrique.
"I take all the responsibility because I chose the first three takers and the players decided the rest - we didn't get to the fourth.
"Bounou is a great goalkeeper when it comes to penalties and today he was superb."
The teams were closely matched throughout a tense game, with Spain having more of the ball but Morocco creating the better openings, few though they were.
Luis Enrique trialled his third right-back of the tournament in Marcos Llorente, and benched the team's top scorer Alvaro Morata for Marco Asensio, after the shock defeat by Japan.
Spain monopolised the ball, with Morocco's fans furiously whistling when their side were out of possession.
Morocco, who are the last African and Arab side left standing at the first World Cup held in an Arab country, were intensely backed and their fans greatly outnumbered Spain's.
With La Roja playing in a pale blue second strip, they resembled the away team on hostile territory.
Gavi, who at 18 years and 123 days old became the youngest player to start a World Cup knockout game since Brazil great Pele in 1958, rose to the occasion.
Regragui's side sat deep defensively, frustrating Spain, and threatened on the counter themselves, with Unai Simon beating away a long range effort from Noussair Mazraoui.
Sofiane Boufal, who bypassed Llorente with ease early on, carved the best chance of the half for Nayef Aguerd, who headed inches over.
Bounou pummelled away a Dani Olmo strike from an angle as the tension cranked up after the break.
Luis Enrique withdrew the dogged and muddied Gavi for Soler.
He threw Morata on too, giving Spain a focal point in attack, but they struggled to supply him as Morocco sat deeper and deeper.
Bounou made a fine save from Olmo's free-kick, with Spain finally creating clear chances in the moments before extra-time.
Morocco found their feet again in extra-time, testing Simon, who made a fine save with his legs to deny Walid Cheddira after he broke down Spain's left flank.
Despite pressure from Spain in the final moments before penalties, they could not find a winner.
Having been sent on for the shoot-out, Sarabia struck the post from Spain's first penalty, after Abdelhamid Sabiri put Morocco ahead.
Soler and Busquets were foiled by Bounou, while Hakim Ziyech scored and Simon denied Badr Benoun, before Hakimi netted a Panenka penalty against the country of his birth to ignite wild celebrations.
"We're devastated because we wanted more, we had the team to do it, but are proud of what we have done," said Llorente.
"It happened to us in the Euros, and today it happened to us again."