The two teams couldn't be separated after 120 minutes and Spain failed to convert any of their three penalties, with goalkeeper Yassine Bounou brilliantly saving two.
That left Paris St-Germain's Hakimi - born and raised in Madrid no less - to win it with his nerveless spot-kick and spark jubilant scenes in the stands.
Education City Stadium was filled mostly by Morocco fans, who went wild at the final whistle as their side reached the last eight for the first time, where they will face Portugal or Switzerland on Saturday (15:00 GMT).
The north African side's fans had been making themselves heard all game but the atmosphere in the stands did not match the attritional battle on the pitch.
Spain hogged the ball and Morocco sat back plugging the gaps, with very few clear-cut chances being created.
Luis Enrique's side came closest to breaking the deadlock courtesy of Dani Olmo, who struck an arrowed drive from the angle which Bounou pushed away and the goalkeeper also did brilliantly well to keep out Olmo's dangerous free-kick late on.
Morocco were playing on the counter and should have scored in the opening period but Nayef Aguerd sent his unmarked header over, while their only shot on target came via Noussair Mazraoui's long-range drive which was gathered by Unai Simon.
With the sides not being able to be separated, the game went into extra time and substitute Walid Cheddira had Morocco's best chance but scuffed his shot straight at Simon from eight yards out.
Spain notched over 1,000 passes in the contest and almost won it in the 123rd minute, but Pablo Sarabia's volley agonisingly flicked the outside of the far post.
It was a cauldron of noise inside the stadium from kick-off as Morocco fans vastly outnumbered their Spanish counterparts and it was the 'Maghreb' who were left celebrating at the final whistle.
Morocco become just the fourth African side to reach the quarter-finals at a World Cup, matching the achievements of Cameroon (1990), Senegal (2002) and Ghana (2010).
Their supporters sang, chanted and banged their drums for the entire 120 minutes, with loud piercing whistles ringing around every time Spain were on the ball.
And Sevilla goalkeeper Bounou seemed to feed of that energy, saving penalty efforts from Carlos Soler and Sergio Busquets, while Sarabia struck the post.
Star player Hakimi stepped up and was the coolest man under the most intense pressure, dinking his penalty - a 'Panenka' - straight down the middle as Morocco surged into the quarters.
Unai Simon was left watching Achraf Hakimi's nerveless penalty
Players ran around the pitch in a daze, manager Walid Regragui had his head in his hands in disbelief at what he had just witnessed, before they lay prostrate on the turf and then jumped up and down in front of supporters.
Morocco's only previous appearance in the last 16 came in 1986, but they have now extended their unbeaten run in the World Cup to five games and kept a clean sheet in six out of their last seven in all competitions.
For 2010 winners Spain, their campaign ends in misery after such an optimistic start when they hammered Costa Rica 7-0 in their opening game.
They tried to pass Morocco into submission but the opposition held firm, the favourites having just one shot on target from their 1,019 passes.
Enrique had said his side needed to do their homework on penalties, practising over 1,000 in training in the lead-up to the match, but they will need to go back to the drawing board after missing all of their spot-kicks.
It is Spain's second successive exit by penalties at the last-16 stage of a World Cup, having also lost to Russia in 2018.
-- Courtesy of BBC Sport