Iran defended doggedly to reach half-time at 0-0 but their resistance was broken by a deflection off Diego Costa in the 54th minute.
Spain is now level on points with Portugal in Group B, while Iran is in third and Morocco now eliminated following their loss earlier in the day.
Diego Costa's deflected goal early has made this World Cup the first one since 1954 without a single 0-0 draw in the first 20 matches.
Spain are level on four points with Portugal a top Group B with the sides holding identical records.
Iran got 3 points in its first game on Friday by beating Morocco 1-0. In its third game, Iran will play with Portugal next week.
The 2018 World Cup started on June 14 in a ceremony attended by dignitaries from various countries.