It appeared as though two teams who had relied upon their defensive records throughout qualifying had cancelled each other out until Aziz Bouhaddouz miscued his headed clearance deep into stoppage time in Saint-Petersberg.
A draw would not have been a good result for either side as they aim to upset the odds in a group which also contains Spain and Portugal, but it was the least that Morocco deserved having dominated the match for long periods.
Herve Renard's side started brightly and had a constant outlet down the right through former Watford winger Nordin Amrabat. While the supply line was initially there, their finishing lacked conviction as both Amine Harit and Ayoub El Kaabi dragged shots wide.
Morocco's best chance fell to Juventus defender Medhi Benatia, who stabbed the ball towards goal during an 18th-minute penalty-box scramble, only to be denied by a combination of Ramin Rezaeian and goalkeeper Alireza Beiranvand.
Once Iran settled into the game, the organisation that helped them keep 14 clean sheets in 18 qualifying games quelled the early Morocco threat, and Carlos Quieroz's side even created a couple of chances of their own before the break.
Alireza Jahanbakhsh, the top scorer in the Eredivisie last season with 21 goals for AZ, scuffed his left-footed shot after being released on the overlap, while Mounir El Kajoui made an important point-blank save from Sardar Azmoun on 44 minutes.
Defences continued to be on top in the second period with Morocco, who themselves conceded only one goal en route to Russia, confident enough in their solidity to commit more bodies forward in search of a winning goal in front of over 60,000 spectators.
Ajax playmaker Hakim Ziyech appeared the most likely to engineer a breakthrough, and he forced Beiranvand into a low diving save with a left-footed volley from 25 yards out, after Younes Belhanda had skilfully teed his teammate up with his head.
However, injuries and stoppages hindered Morocco's momentum and a clumsy late foul from Sofyan Amrabat, who had earlier replaced his potentially-concussed brother, allowed Iran to load the box and pinch a famous victory, at the expense of the unfortunate Bouhaddouz.
The late twist ended Morocco's 18-match unbeaten run, and spells trouble for their World Cup campaign with two European heavyweights still to come.
Meanwhile, Iran are the unlikely early leaders of the toughest group at the tournament, following only their second ever win at the Finals.
MOROCCO (3-4-2-1): El Kajoui; Achraf, Benatia, Saiss; Amrabat (Amrabat, 76'); El Ahmadi, Boussoufa, Harit (Da Costa, 82'); Ziyach, Belhanda; El Kaabi (Bouhaddouz, 77')
IRAN (3-4-2-1): Beiranvand; Hajisafi, Cheshmi, Pouraliganji; Rezaeian, Ebrahimi (Hosseini, 81'), Ansarifard, Shojaei (Taremi, 68'); Amiri, Jahanbaksh (Ghoddos, 85'), Azmoun