Morocco eliminated Spain from the Qatar World Cup in a stunning penalty win to reach the quarter-finals for the first time on Tuesday.
Goalkeeper Yassine Bounou saved spot kicks from Carlos Soler and Sergio Busquets after Pablo Sarabia had hit the post.
Paris Saint-Germain defender Achraf Hakimi — who was born and raised in Spain and had started his career at Real Madrid — clinched victory for the Atlas Lions with a Panenka-like chip into the middle, after Abdelhamid Sabiri and Hakim Ziyech had converted while Badr Benoun's effort was saved by Spain keeper Unai Simon.
The score was locked at 0-0 after 120 minutes in which scoring chances were sparse, although Sarabia almost stole victory for Spain when he hit the post for the first time seconds before the shoot-out.
"I'm so happy for the team. They did a great job for the whole of the game. The team did a great job, an incredible job. All the players," Bounou said.
Busquets said: "It was really cruel how we lost. Really cruel. It was a Moroccan wall. We were looking for the opening but we didn't find it. We were just unlucky."
"A historic victory, I think the people at home are very happy," forward Walid Cheddira said.
They had already impressed in the group stage where they beat Belgium 2-0 and Canada 2-1 after holding the 2018 runners-up Croatia to a 0-0 draw.
No opposing player has managed to beat them in Qatar as the only goal they have conceded was an own goal by Nayef Aguerd against Canada.
Spain are meanwhile without a knock-out victory in three World Cups since lifting the trophy in 2010 against the Netherlands. They went out in the group stage in 2014 and in the last 16 four years later on penalties against hosts Russia.
They started with an impressive 7-0 against Costa Rica but never won again, drawing 1-1 with Germany and losing 2-1 against Japan before the exit against Morocco.
At age 18 years and 123 days, Spain midfielder Gavi became the youngest player since Brazil legend Pele in the 1958 final (17 years 249 days) to start in a World Cup knock-out game, after also featuring in all group games.
But he and his team-mates badly lacked ideas as they found no way through the stubborn Morocco defense, with Marco Asensio hitting the side-netting in the 27th.
Bayern Munich's Noussair Mazraoui tried his luck from a distance at the other end but the ball was gathered by Simon, and Aguerd headed over from a promising position, set up by the lively Sofiane Boufal.
After the break, Dani Olmo's left-angle free-kick was fisted away by Bounou as Spain tried in vain to up the pressure.
The Atlas Lions had a rare attack with some five minutes left but Cheddira failed to get a dangerous shot toward goal, and Simon then just got the ball away after a backpass ahead of the charging Abdessamad Ezzalzouli.
In stoppage time, Morata's poor header went well over, and Bounou just palmed away another curling Olmo free-kick which two Spain players had missed.
Cheddira then failed to beat Simon from close range when he should have done better - which almost backfired when Sarabia fired Rodrigo's cross against the far post from a tight right angle a minute after coming on.
And when he hit the post again in Spain's first attempt in the shoot-out it set the stage for La Roja drawing a complete blank and joining Switzerland in 2006 as the only teams failing to score in a World Cup shoot-out.
Morocco advanced to a quarter-final clash with either Portugal or Switzerland who meet later on Tuesday. It became the fourth African country to ever reach the last eight and the first since Ghana in 2010.