The victory in Kazan puts the 2010 winners on course for the last 16, but Iran had an equaliser disallowed for a narrow offside.
“We have three points… but it was very difficult and complex game. Both halves were very tough,” said Hierro.
“We started well, with good pace, but Iran are a very tough team, it's very difficult to score against them.”
Costa, who scored twice in a 3-3 opening draw with Portugal in Sochi, courted controversy early in the Group B encounter when he stepped on the toes of Iran 'keeper Ali Beiranvand.
His gesture went unpunished, and the Atletico Madrid striker required another stroke of luck on 54 minutes when his shot came off Ramin Rezaeian to beat Beiranvand after he had been sent through by Andres Iniesta.
Just before the hour mark, the entire Iranian bench poured onto the pitch when Saeid Ezatolahi fired an equaliser for Iran.
But the thousands of Iranian supporters that filled the stadium with the sound of vuvuzelas throughout were left disappointed when it was ruled out for offside.
With three points from two matches, Iran still have a chance of qualifying if they can get a result against Cristiano Ronaldo's Portugal on Monday.
'Can be proud'
Iran coach Carlos Queiroz hailed the bravery of his side and pledged they would fight to make the last 16.
“We left the pitch 1-0 down, but we won respect. I'm sure the fans are proud of our team,” said Queiroz.
“Our players can go home and sit with their family and friends and be proud of themselves.
“People gave us no chance, but we still have chances to qualify for the second round. We'll fight for our dreams.”
Hierro welcomed Real Madrid's Dani Carvajal back into the starting line-up following his recovery from a hamstring injury suffered in the Champions League final win over Liverpool.
With Lucas Vazquez replacing Atletico Madrid midfielder Koke, it gave La Roja a distinctively Real Madrid feel.
Costa got so frustrated with Iran's defensive tactics that he approached Beiranvand and provocatively stepped on his toes, leaving the 'keeper in visible pain.
It went unpunished by Uruguayan referee Andres Cunha, who had awarded France a historic first VAR-assisted penalty to France in their 2-1 win over Australia.
Spain resumed in more positive fashion after the break, Beiranvand forced to produce heroics twice in the space of four minutes.
But Iran refused to buckle, coming close in the final minutes when midfielder Mehdi Taremi headed just over.
Shortly after the restart an Isco corner was flicked on by Costa to Gerard Pique but his effort was cleared off the line.
Sergio Busquets' drive from 20 yards was then parried by Beiranvand, who palmed the rebound away before Lucas Vazquez could pounce.
Some great vision from Iniesta, and a slice of luck, broke Iranian hearts when Costa spun and latched on to the ball before seeing his shot rebound off Ramin Rezaeian and into the net.
Spain, however, were still flattering to deceive. On a rare Iranian counter-attack, goalkeeper David De Gea was left irate when Taremi was left to head a metre wide.
Moments later, the stadium erupted when Ezatolahi fired through a packed Spain area to beat De Gea, but the assistant referee's flag was up.
In the final quarter hour Iran battled for a late equaliser, but after Vahid Amiri nutmegged Pique and fired over an inviting cross, Taremi's thundering header flew just over the bar.
By AFP''sJustin Davis