The 2010 champions started Monday on top of Group B but seemed certain to slip below their rivals in the final table as both matches entered stoppage-time with Portugal and Morocco leading.
Iniesta soon made amends, though, skipping through the Morocco defense to set up Isco, with Spain unable to break down their opponents despite dominating.
When Morocco substitute Youssef En-Nesyri got up highest to thump home a brilliant header from a Faycal Fajr corner with nine minutes to go, Herve Renard's men looked set to claim a memorable victory.
But after another substitute, Iago Aspas, tucked in a superbly improvised finish that survived a lengthy VAR review for offside, while Iran struck a late penalty, Spain somehow finished top of the group.
Spain showed signs of its bewitching best in possession but Fernando Hierro's side must show serious improvement in the knockout stages if they have ambitions to claim their second World Cup.
A testy start to the game saw Gerard Pique perhaps fortunate to avoid punishment for a two-footed lunge on Boutaib.
Morocco's lone forward then gave his side a shock lead in the 14th minute, capitalizing on a terrible mix-up between Ramos and Iniesta.
The duo's shared lapse in concentration allowed Boutaib to steal possession on the halfway line, burst through on goal and put a neat finish through the legs of David de Gea.
That goal was Spain's wake-up call, Iniesta and Diego Costa combining quite brilliantly to set up Isco for a slammed close-range finish that tied the scores five minutes later.
Boutaib scampered clear again in the 25th minute when Morocco took a quick throw-in, but De Gea — beaten from all four shots on target he had faced at the World Cup — stayed tall to save.
Morocco threatened to lose its discipline with Nordin Amrabat, Manuel da Costa and Moubarak Boussoufa all booked in a tempestuous three-minute period before Sergio Busquets planted a free header from Isco's corner over the crossbar.
4 - Morocco were shown four yellow cards in the first half agaist Spain - since 1966, the only other occasion of a team having more in the first half of a World Cup game was also by Morocco (5 v Netherlands in 1994). Crunch. #ESPMAR #MAR #WorldCup pic.twitter.com/DuEfNbOIRr — OptaJoe (@OptaJoe) June 25, 2018
Pique was lucky again early in the second half when the defender appeared to handle the ball in the box, with Morocco agonizingly close to regaining the lead in the 55th minute.
Given space to let fly from at least 25 yards out, Amrabat's strike had De Gea completely beaten but the goalkeeper gratefully saw the ball bounce off the woodwork.
Isco thought he had added to his nine Spain goals scored since the start of 2017 with his looping header dipping under the crossbar, but Romain Saiss made a sensational clearance, Pique heading wide from the resulting Isco corner.
Hierro introduced Marco Asensio and Aspas as Spain sought a late winner but Morocco's determined defending held them at bay.
Although only Iran shocking Portugal could have dumped Spain out, En-Nesyri thundered home a stunning header that left De Gea helpless again.
And when Aspas turned home Dani Carvajal's cross after a short corner — the goal given by VAR after a long delay— Spain was tied in the first minute of added time.
With more high drama elsewhere — Karim Ansarifard smashing home an equalizing spot-kick for Iran — Spain leapfrogged Portugal to Group B's summit and will now play Russia in the last 16.