Gaitan, who netted a penalty in his side's opening 1-1 draw with Great Britain, scored both goals at the Riverbank Arena to put Spain within touching distance of the semi-finals.
The midfielder's double came in the first half and saw him cut in from the left each time against an Iran side who went on to dominate most of the second half but could not find a goal and as a result cannot now progress to the knockout stages.
'We are very happy to have won. Obviously those three points were very important to us. Now we can be a bit calmer about the next stage,' said Gaitan.
'It's obviously great to score goals and I'm really happy to have scored them, but the main thing is that Spain does well. We just want to win the gold medal.'
Spain will progress if they win their last group game against Argentina, who were held to a 0-0 draw by Great Britain. The South Americans had the better of the chances in the match and David Peralta almost scored the goal that would have put Argentina through to the semi-finals with five minutes left but struck wide when well placed.
Britain must win their final group match against Iran on Tuesday to have a chance of going through, while Argentina know a win against Spain will guarantee them progress - although a draw in this match could mean goal difference is needed to decide the top two places.
In Group B, Brazil turned on the style as they beat Turkey 4-0. Jeferson Goncalves, who scored twice, and Ricardo Alves, who scored once, were in impressive form and the Turkish defence could not handle them or Brazil's passing game.
Severino da Silva scored the other goal from a penalty, although Brazil's tally could have been much higher had they not missed four other penalties.
Defending champions Brazil now face China looking to secure their semi-final place, and coach Ramon Pereira de Souza said: 'We expected to score more than four goals, missing the penalties was not expected. China has four goals, this is why we had three attackers, in preparation to score many goals.'
The Chinese, meanwhile, drew 0-0 with France, who have recorded goalless draws in both of their games.
In what is another tight group, a French win over Turkey would see them go through if there is a winner in the Brazil versus China match - although if the match finishes level then all three teams could finish on five points and it would come down to goal difference.