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.