Some great vision from Iniesta, and a slice of luck, however broke Iranian hearts when Costa spun and latched on to the ball before seeing his shot rebound off Ramin Rezaeian and into the net.
Iran remain on three points and can reach the knockout rounds for the first time with victory over Portugal.
"I know Fernando since many years ago", Isco told reporters in Krasnodar on Monday.
Facing a very compact Iranian team that came out to defend, Spain pressed high and controlled much of the game but found it hard to threaten against Iran's 10-man defence.
And while goalkeeper Alireza Beiranvand recognises few will fancy Iran to add to their current points tally, he is in a defiant mood and insists Spain will be in for a battle.
With less than 30 seconds to go of injury time in the final moments of the match and Iran trailing Spain 1-0, David De Gea's side found themselves facing a throw in deep into their own half.
Spain was mostly unsafe from set-pieces, including in the 25th minute when a free kick from David Silva that took a deflection was stopped by Iran goalkeeper Ali Beiranvand.
The Asian side nearly scored the most unlikely of openers when Karim Ansarifard's instinctive effort from a corner flashed into David De Gea's side netting.
VAR was called into action and showed an Iranian player was offside and the goal was disallowed.
La Roja were their usual composed selves in possession but were frustrated by Carlos Queiroz's well-organised defence through the opening 45 minutes, with over 80 percent of possession bringing just one shot on target. He has scored three goals in two matches, but he can do even better.
Iran, making their fifth appearance in the World Cup final, face Cristiano Ronaldo's Portugal in Saransk five days later.
Any thoughts that Iran would be blown away by their more illustrious opponents soon dissipated as their expertly drilled defence comfortably rebuffed wave after wave of Spanish attack in the opening stages. The Iranians were awarded a freekick which they fail to goal in on!
Dermot Corrigan is a Madrid-based football writer who covers La Liga and the Spain national team for ESPN FC.
Spain's pre-tournament coaching debacle marred the start of their campaign, with Julen Lopetegui quitting 24 hours before Russian Federation 2018 commenced - but they remain amongst the favorites for the title due to the strength of their current playing squad.
Spain is now unbeaten in 22 matches, with 15 of those wins. The team's last loss was against Italy in the round of 16 at the 2016 European Championship. In fact, the manner of their return to the pitch for the second half - with players closely grouped together and patting each other on the back - gave every indication they were just as united as ever.
Spain's frustration was in danger of boiling over shortly before the interval when Costa got involved in an unnecessary spat with Beiranvand, although referee Cunha opted not to punish the combative striker.
How do I watch live stream of the FIFA World Cup 2018 Iran vs Spain match in India? "They were provoking us, and we were on the grass all the time".