The victory has seen Spain join Portugal on four points in Group B, while Iran remain third on three points ahead of the final matchday on June 25.
As for Iran, head coach Carlos Quieroz made three changes to the team that started the 1-0 win over Morocco. Majid Hosseini, Mehdi Taremi and Saeid Ezatolahi were all included, while Sardar Azmoun kept his spot at the tip of the attack.
Carvajal had the game's first half-chance in the fourth minute, but the right-back's effort from an advanced position was always too high.
The noise inside the stadium was absolutely deafening as the first half gathered pace, although opportunities were few and far between, with the first 20 minutes passing without major incident.
David Silva tested Iran goalkeeper Alireza Beiranvand with a curling free kick in the 25th minute, before the Manchester City attacker fired an overhead kick over the crossbar 15 minutes before the end of the first period.
It was an attack versus defence exercise as Iran continued to camp deep, and they were making it almost impossible for Spain to pick their passes in a forward position.
Silva had Spain's best chance of the first period in the closing stages, but the attacker saw his attempt deflected wide of the post as Iran's rear-guard action saw the two teams head down the tunnel at 0-0.
Spain had more than 80% of the ball in the opening 45 minutes, and it did not take them long to gain control of the possession in the second period.
La Roja had two chances in the early stages of the second half, but Gerard Pique saw his goal-bound effort cleared before Iran goalkeeper Beiranvand kept out a long-range attempt from Sergio Busquets.
Isco was next to come close for Spain in the 52nd minute, with the Real Madrid midfielder clipping over the crossbar following smart work from Carvajal down the right.
Karim Ansarifard smashed one into the side-netting of the Spain goal in the 53rd minute as Iran came close, but Quieroz's side fell behind in unfortunate circumstances less than one minute later.
The goal was given to Costa, but the Atletico Madrid forward needed a huge slice of luck as Iran defender Hosseini smashed the ball against the 29-year-old inside the box, and it dropped into the bottom corner of the net.
Taremi headed wide of the post on the hour as Iran looked to hit back, and they had the ball in the back of the Spain net in the 62nd minute courtesy of Ezatolahi, but VAR ruled the midfielder offside.
Iran managed to survive conceding a second in the 70th minute following a scramble inside their penalty box, and there was still everything to play for entering the final 20 minutes of action in Kazan.
Taremi had a wonderful chance to level the scores in the 82nd minute after meeting a cross from the left, but Iran's number 17 somehow planted his header over the crossbar from close range.
Spain had the ball for the majority of the match, and that was also the case in the latter stages as they closed out the contest to secure all three points. Hierro's team will finish their group campaign against Morocco on June 25, while Iran take on Portugal on the same night.
IRAN (4-5-1): Beiranvand; Haji Safi (Mohammadi 70'), Pouraliganji, Hosseini, Ramin; Taremi, Ezatolahi, Omid, Amiri (Ghoddos 86'), Karim (Jahanbakhsh 75'); Sardar
SPAIN (4-2-3-1): De Gea; Carvajal, Pique, Ramos, Alba; Busquets, Iniesta (Koke 71'); Silva, Isco, Vazquez (Asensio 79'); Costa (Rodrigo 89')