Luis Enrique's side dominated possession throughout the game, but could not break down a stubborn Moroccan defence.
The best open play chance of the game fell in stoppage time of extra time, as substitute Pablo Sarabia grazed the far post with an unmarked volley.
Story of the Match
Both sides started the game in a 4-3-3 formation. Morocco made one change to the side that beat Canada 2-1, with Selim Amallah replacing Sabiri in the starting 11.
Both sides looked to have settled early in the game, resulting in neither giving away early chances. Marco Asensio's 26th minute shot was the first real chance of the game as the Real Madrid man hit the side netting.
Morocco sat deep for the majority of the game, looking to negate Spain's quality when in possession and catch their opponents on the break.
Spanish frustration began to set in after the break. Dani Olmo had Spain's only effort from range in the match, as his fierce free kick forced goalkeeper Bounou to punch clear.
20-year-old Williams began to create chances by picking gaps in the Moroccan back line. Both Olmo and Alvaro Morata failed to capitalise on late opportunities created by the winger.
As Spain pushed men forwards, Morocco sat deeper, soaking up any pressure. Olmo's dipping effort at goal was pushed away by Bounou in the final act before extra time.
Moroccan substitute Walid Cheddira was the recipient of two golden opportunities in extra time.
Five minutes after the restart, the striker found himself through on goal, but his uncertainty allowed Aymeric Laporte to recover and deny him an effort at goal.
The striker had an even better chance 10 minutes later, holding off Alex Balde to get a shot on goal, only to be denied by the outstretched left leg of Unai Simon.
The second half of extra time saw Morocco pinned into their own defensive third, as both Spanish centre halves played possession football inside the Moroccan half.
Regragui's men stood firm, starving Spain of any significant chances and seemingly taking the game to penalties.
With the final attack of the game, Carlos Soler's cross found an unmarked Sarabia at the far post. The 30-year-old's side footed volley flew across the face of Bounou's goal, glancing the far post in the 123rd minute and sending the game to penalties.
Sarabia's luck would not improve. After Sabiri scored Morocco's first penalty, the Paris Saint-Germain winger would strike the post, handing Morocco an early advantage.
Spain looked to have a glimmer of hope when Simon denied Badr Benoun, only for Bounou to palm away Sergio Busquets' follow up effort.
With Morocco 2-0 ahead, a goal from Hakimi would seal their spot in the quarter-final. The fullback calmly chipped his penalty down the middle of the goal as Simon dived out of the way.
Morocco now have four days to rest before their next game.
Player of the Match
It is not everyday you save two penalties in a World Cup shootout, making history for your nation in the process.
That is exactly what Yassine Bounou did tonight, writing his name into Moroccan footballing history.
The Sevilla man also put in a solid performance in regulation time, denying Spanish attackers on numerous occasions to keep his side in the tie.