La Roja had not accounted for Morocco's remarkable endeavour, though, as Herve Renard's side looked to have snatched a shock victory when substitute Youssef En-Nesyri thumped a header home in the 81st minute.
A thrilling encounter on the Baltic Coast was not over just yet, though, as Spanish sub Iago Aspas saw his goal awarded by VAR in stoppage time, after originally being given offside, in order for Fernando Hierro's charges to steal a memorable point.
Elsewhere in the group, Portugal were forced to settle for a draw against Iran, leaving Spain to eventually top the standings and establish a last-16 clash with tournament hosts Russia.
It took just a few more minutes for another flashpoint to develop, and Spain might have considered themselves lucky to keep 11 men on the pitch when Gerard Pique and Boutaib clashed.
Dani Carvajal sold Pique short with a hospital pass and the centre-back responded by flying in on Boutaib in an attempt to win the loose ball. Replays suggested that the Barcelona defender might just have got there first, but on another night he could have been given his marching orders with two feet raised.
It was a defensive mishap that then led Morocco to take a surprise lead, as Sergio Ramos and Iniesta were caught dawdling by Boutaib just inside the Spain half. The striker stole in to snatch possession, before racing away to slot coolly past David de Gea in the 14th minute.
La Roja responded in typical Spanish style, however, as Isco levelled only five minutes later. Iniesta was the architect as he rounded his marker inside the box, leaving the playmaker to cut the ball back for Isco, who rifled into the roof of the net.
Morocco were unable to let things lie despite already being eliminated from the tournament, and they almost had a second courtesy of more defensive laziness from Ramos and company. Boutaib was again released into space after a quick throw-in, but this time De Gea stood tall in order to make a standout save with his feet.
Spain took possessional control of the encounter heading into the interval and could have gone into the lead before the break, Sergio Busquets rising highest to meet a corner and send a free header over the bar from close range.
Renard's men continued to keep Spanish nerves jangling in the second half, and Nordin Amrabat thought he had put Morocco into the lead, only to see his strike from range cannon back off the woodwork with De Gea beaten all ends up.
La Roja continued to go about their attacking work undisturbed and a second goal should have arrived when Isco rose inside the area, leaving Romain Saiss to valiantly head clear off the line to prevent an almost-certain goal.
The interception turned out to be crucial, though, as substitute En-Nesyri pulled off the unthinkable with nine minutes to play, beating Ramos at a corner to send a powerful header past De Gea at the death.
It looked as though Morocco were to seal a shock victory, only for VAR to interfere and hand Spanish substitute Aspas a stoppage-time goal that was originally ruled out for offside by referee Ravshan Irmatov.
Iran's late heroics against Portugal elsewhere in the group then saw Fernando Santos and his men give up first place to La Roja, who went from the brink of a defeat and second spot, to stealing a point and rising straight to the top of the standings within a couple of outlandish minutes.
SPAIN (4-2-3-1): De Gea; Carvajal, Pique, Ramos, Alba; Busquets, Thiago (Asensio 74'); Isco, Silva (Rodrigo 84), Iniesta; Costa (Aspas 74')
MOROCCO (4-2-3-1): Kajoui; Hakimi, Da Costa, Saiss, Dirar; El Ahmadi, Boussoufa; Belhanda (Fajr 63'), N. Amrabat, Ziyach (Bouhaddouz 85'); Boutaib (En Nesyri 72')