A lucky Diego Costa goal handed World Cup hopefuls Spain their first win of the World Cup although dogged Iran came close to causing a major upset on Wednesday. The victory in Kazan puts the 2010 winners on course for the last 16. 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 as he waited to kick the ball out. 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.
The entire Iranian bench poured onto the pitch when Saeid Ezatolahi fired an equaliser for the Lions of Persia just before the hour.
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.
Spain coach Fernando 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.
By the end of a pulsating opening half in which Iran's defence thwarted the few real chances La Roja were able to create, Hierro's men left the pitch looking rather red-faced.
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 Cunha, who had awarded France a historic first VAR-assisted penalty that saw Antoine Griezmann score Les Bleus' opener in a 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, Carlos Queiroz's men 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, 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.
Spain, however, were still flattering to deceive.
On a rare counter goalkeeper, David De Gea was left irate when Taremi rose 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 showed class to nutmeg Pique deep on the left and fire over an inviting cross, Taremi's thundering header flew just over the bar as a relieved De Gea looked on.