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.
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.
