Carlos Queiroz’s side produced a defensive masterclass to reduce Spain to nothing more than half-chances during a hugely frustrating opening 53 minutes for the 2010 champions.
Costa – who scored twice in Friday’s pulsating 3-3 draw with Portugal – eventually found the breakthrough, though, when Ramin Rezaeian’s attempted clearance cannoned off his leg and into the bottom-left corner.
Iran had the ball in the back of the net just eight minutes later courtesy of Saeid Ezatolahi, but their wild celebrations were cut short after referee Andres Cunha was advised by VAR that Rezaeian’s vital intervention had been made from an offside position.
Iran’s approach was far more positive following Costa’s strike, Mehdi Taremi heading just over from close range in what was their clearest opportunity to find an unlikely leveller.
The win moves Spain level on points with Portugal at the top of Group B and they know a draw against Morocco on Monday will be enough to secure qualification for the knockout stages. Iran, meanwhile, face Portugal on the same day needing a win to progress at the expense of Cristiano Ronaldo and Co.
Iran’s resistance was nearly broken twice in quick succession at the start of the second period.
First, Pique’s close-range effort was blocked and then Beiranvand palmed away Sergio Busquets’ curled effort from the edge of the penalty area.
The Asian side almost scored the most unlikely of openers when Karim Ansarifard’s instinctive effort from a corner flashed into David De Gea’s side netting.
That near-miss was brought into sharp focus less than 60 seconds later when Costa picked up Andres Iniesta’s pass inside the area and saw Rezaeian’s attempted clearance ricochet off his leg and past the helpless Beiranvand.