Iran won their first match at a World Cup finals since 1998 as Morocco substitute Aziz Bouhaddouz scored an own goal in injury time.
Bouhaddouz put the ball into his own net with a diving header at the near post in the 95th minute as Morocco defended a left-wing free-kick.
The result was harsh on the African side, who had wasted numerous chances in the first half and were denied late on when Iran goalkeeper Alireza Beiranvand produced a fine save from Hakim Ziyech.
But Iran took the victory, only their second in World Cup history after a 2-1 win over the United States at France 1998.
Morocco became the second African side to concede a late winning goal in the World Cup on Friday, after Egypt lost 1-0 to Uruguay in Group A, thanks to a last-minute header from Jose Gimenez.
Relive Morocco versus Iran as it happened
A vital three points for Iran
Both Morocco and Iran impressed in qualifying with Morocco only conceding one goal in their eight games, while Iran remained unbeaten and only let in five goals in their 18 qualifying matches.
However, they were both left rueing the draw that saw them placed in the same group as 2010 winners Spain and European champions Portugal, with only the top two sides advancing to the last 16.
Spain and Portugal, 10th and fourth in the world rankings respectively, have won four major tournaments between them in the past 10 years. Before the match, Iran’s coach Carlos Queiroz said his side had been “drawn in the hardest group in the World Cup”.
Going into injury time in St Petersburg, it looked like being a result neither Morocco or Iran wanted, before Morocco’s Sofyan Amrabat conceded a needless free-kick by fouling Saman Ghoddos near the corner flag.
But that gave Ehsan Haji Safi the opportunity to whip in the free-kick, and Bouhaddouz could only steer the ball into his own net for an unlikely Iran victory.
Morocco will now need a positive result when they play Portugal on Wednesday, 20 June (13:00 BST), before Iran play Spain at 19:00 BST on the same day.
A bad week for Moroccan football
Earlier this week, Morocco failed in their attempt to win the right to host the 2026 World Cup, losing out to a ‘United’ bid from Canada, the United States and Mexico.
The King of Morocco, King Mohammed VI, has already instructed his country to try again and bid to host the 2030 event, while a strong World Cup showing in Russia would help raise the profile of the team.
However, barring unlikely wins against either Portugal or Spain, they are now facing an early exit after failing to take their chances.
They started at a frantic pace, having four good opportunities inside the opening 10 minutes.
Amine Harit pulled a half-volley wide, Ziyech missed his kick when an inventive free-kick routine saw the ball rolled to him on the edge of the penalty area, Younes Belhanda sent a diving header wide and Ayoub El Kaabi shot off target.
The chances kept coming for the African side as Belhanda had an effort blocked in a goalmouth scramble with Iran goalkeeper Beiranvand saving Mehdi Benatia’s follow-up strike.
But, despite Morocco having 68% of possession and 13 shots, Iran picked up the victory.
Iran win despite boot drama
Iran were unbeaten in all 18 of their qualifying matches.
However, their preparations for the tournament were disrupted on Tuesday when Nike said it had withdrawn its supply of boots to Iranian footballers before the World Cup because of new US sanctions.
It did not matter on Friday as a hard-working Iran side, helped by a combination of poor Moroccan finishing, desperate defending and solid work from Beiranvand, survived early pressure to keep the game goalless.
Indeed, Iran had the best chance of the first half when Azmoun was put clean through, with only Morocco keeper Munir Mohamedi to beat.
But the chance was wasted as Azmoun did not show enough composure with Mohamedi denying him and also saving the follow-up attempt from Alireza Jahanbakhsh.
However, it did not matter as Queiroz, who will end his seven-year stint as Iran manager after the tournament, celebrated with his coaching staff and his players at the final whistle.