Spain missed all three of its spot kicks as Moroccan goalkeeper Bono made himself the hero of the shootout, saving efforts from Sergio Busquets and Carlos Soler, while Pablo Sarabia hit the post.
Achraf Hakimi, who was born in Spain, buried the winning penalty with the cheekiest of Panenkas, chipping the ball gently into the middle of the goal to send the thousands of Moroccan fans inside Education Stadium into dreamland.
Defeat for Spain continues its wretched World Cup record since lifting the trophy in 2010, with La Roja managing just three tournament wins – against Australia, Iran and Costa Rica – in the three editions since that triumph.
Ahead of Tuesday’s game, Spain coach Luis Enrique said that he had given his players “homework” a year ago to practice 1,000 penalties at their clubs, but when the heat of the moment arrived, Busquets, Soler and Sarabia cracked under the pressure.
Not so Hakimi, whose nerveless dink sparked wild scenes on the pitch as the Paris Saint-Germain full-back was mobbed by his teammate and coaching staff, who celebrated Morocco becoming the first Arab nation to reach the World Cup quarterfinals.
This Morocco team has been a revelation this tournament, defending valiantly and attacking with verve and fans of the North African national team will rightly feel confident of getting past either Switzerland or Portugal in the next round to create yet more history.
Spain produced arguably the most eye-catching result of the opening round of fixtures, dismantling Costa Rica 7-0 in an utterly dominant performance to put all the other World Cup contenders on notice.
In its next two matches, however, Spain stuttered to a draw against Germany and was then shocked by Japan, eventually qualifying for the round of 16 in second place behind the Samurai Blue.
Morocco, then, could be forgiven for feeling slightly aggrieved at drawing Spain in the knockout stages after qualifying comfortably top of its group.
But the Atlas Lions have been without a doubt among the most impressive performers so far in Qatar and will have justifiably felt no fear at all going into this clash against La Roja.
Morocco’s head coach Walid Regragui had told his players before this game to “aim for the sky” and they started the match with an intensity that showed they were not going to be overawed by the occasion or their World Cup-winning opponent.
Red Moroccan shirts were snapping into tackles inside 30 seconds, never allowing the Spanish players a moment’s peace on the ball.
Morocco’s fans – who have been some of the best in Qatar, making each game almost feel like a home fixture – responded vociferously, roaring on every Moroccan tackle and jeering loudly every time Spain was in possession of the ball.
Morocco looked like the most dangerous team inside the opening 20 minutes, too, causing Spain’s defense problems on the break, with one of those counterattacks forcing Busquets to bring down Sofiane Boufal.
Hakimi, who was born in Spain to Moroccan parents, flashed the subsequent free-kick over Unai Simón’s crossbar.
Perhaps Morocco’s early dominance perhaps made its players overconfident, as some casual passing out from the back resulted in a glorious chance in front of goal for Gavi, but goalkeeper Bono recovered brilliantly to tip the forward’s effort onto the crossbar without knowing the linesman’s flag had gone up for offside.
It was a first warning for Morocco that any lapse in concentration could be punished at any moment by Spain’s talented front line.
Just three minutes before half time, Morocco had the best chance of the game so far.
Created by wide man Boufal, who had impressive match, his cross to the far post was headed over the crossbar by Naif Aguerd, who really should have at least got his effort on target.
As brilliant as Morocco had been in the first half, finishing the opening 45 minutes with a flourish, Spain had been equally as disappointing. As so often in this World Cup, Enrique’s side had enjoyed considerably more possession, but most of it had been ponderous passing 10 yards from the Moroccan penalty area.
There would have likely been a few choice words at half time from Enrique to his players, who came out in the second period with considerably more intensity.
Morocco barely had a touch of the ball in the attacking half, as the light blue Spanish shirts swarmed their opponents every time they lost possession.
Though Morocco’s defense was certainly looking more stretched – Aguerd at one point put in a brilliant last-ditch tackle on Alvaro Morata inside the penalty area – for all of Spain’s possession, which reached a staggering 85% in the second half, Bono in the Moroccan goal was yet to be seriously troubled.
It took until the 81st minute for Spain to conjure its first chance of note, as substitute Nico Williams’ through ball set Morata away but the forward’s flashed shot across goal was poked away by Bono’s toe with not another Spanish player anywhere near the Moroccan goal.
It felt increasingly inevitable that this game was heading to extra time, but Spain did have one final chance to win it. Dani Olmo’s whipped cross into the box missed Williams’ toe by millimeters and Bono was able to punch the ball behind for a corner.
In what was a predictably cagey first half of extra time, Morocco had a glorious opportunity to take the lead but substitute Walid Cheddira hit his effort straight at Simón after being put through one-on-one by Hakim Ziyech.
Bizarrely, Spain finally seemed to wake up in the dying moments of extra time, heaping pressure on the Moroccan goal until Rodri’s brilliant cross found Sarabia unmarked in the box, but the Paris Saint-Germain forward struck his volley against the far post.
Sarabia was made to rue that miss as his night went from bad to worse, striking his penalty in the shootout against the post with Spain’s first effort.
It was a sign of things to come for Spain, with Busquets and Soler also failing to score.
Despite Badr Banoune missing his penalty for Morocco, Abdelhamid Sabiri, Ziyech and then, coolest of them all, Hakimi buried their spot kicks to book their place in Moroccan football folklore.