Achraf Hakimi struck the decisive penalty to send Spain crashing out and Morocco into the World Cup quarter-finals for the first time, winning 3-0 in a shoot-out on Tuesday after a 0-0 draw.
Spain coach Luis Enrique had claimed his players prepared by taking 1,000 penalties as homework but Pablo Sarabia, Carlos Soler and Sergio Busquets all missed from the spot and Madrid-born Hakimi dinked his home to the delight of Morocco's raucous support.
Walid Regragui's side will face Portugal or Switzerland in what is uncharted territory for them, with their previous best 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.
The teams were closely matched through a nail-biting clash, 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 with their side 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.
The Barcelona midfield terrier was at his most intense, smashing into challenges left, right and centre, even diving to make one with his head.
Morocco goalkeeper Yassine Bounou tipped a Gavi strike on to the crossbar, though it would have been ruled offside, while Asensio drilled into the side-netting, although Spain created little.
Regragui's side sat deep defensively and threatened on the counter, with Unai Simon beating away a long range effort from Noussair Mazraoui.
Sofiane Boufal, who bypassed Llorente like sand slipping through the Spaniard's fingers 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 raged at Rodri for choosing the wrong option on the ball, and withdrew the dogged and muddied Gavi for Carlos Soler.
He threw Morata on too, giving Spain a focal point up top, but they struggled to supply him as Morocco sat deeper and deeper.
Another substitute, Nico Williams did find the Atletico Madrid striker on one occasion but the angle was too tight and he scraped a shot across the face of goal.
Bounou made a fine save from Olmo's free-kick as it almost snuck all the way through, with Spain finally creating stronger chances as the game frayed in the moments before extra-time.
Morocco responded in the additional period by finding stability and testing Simon, who made a fine save with his legs to deny Walid Cheddira after he broke in down Spain's left flank.
Despite heavy pressure by Spain in the final moments before penalties, they could not trouble the Sevilla goalkeeper again, with Pablo Sarabia clipping the outside of the post, although he might have been offside.
Having been sent on apparently for the shoot-out, Sarabia struck the post again from Spain's first penalty, after Abdelhamid Sabiri had sent Morocco ahead.
Soler and Busquets missed, while Hakim Ziyech scored, before Hakimi netted against his birth country to ignite wild celebrations. (AFP)