The substitute 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.
Morocco wasted numerous chances in the first half and were denied late on when Iran goalkeeper Alireza Beiranvand produced a fine save from Hakim Ziyech.
The win for Iran became 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.
CLICK ON THE LINK BELOW TO WATCH THE HIGHLIGHTS: