Cristiano Ronaldo scored a hat-trick as Portugal and Spain drew a thrilling  World Cup Group B game 3-3 in Sochi on Friday night.

Ronaldo’s last goal was a spectacular free kick, just when it looked as if two goals from Diego Costa and a volley from Nacho Fernandez would give Spain a winning start to their World Cup campaign, just two days after Fernando Hierro replaced Julen Lopetegui as coach.

Despite Ronaldo’s late goal the Spanish can take heart that the change of coach doesn’t appear to have had any negative effects on their play and both sides look much stronger than both Iran and Morocco, who played each other in Group B earlier in the day.

New Spain coach Fernando Hierro decided to play Costa in attack, while Koke partnered Sergio Busquets in midfield and Nacho Fernandez was given the task of stopping his Real Madrid clubmate, Cristiano Ronaldo.

Portugal meanwhile fielded an attacking line up with Ronaldo, Goncalo Guedes and Bernardo Silva in the starting 11.

The game was only 4 minutes old when Ronaldo scored from the penalty spot after drawing a foul from Nacho, though TV replays implied it was the Portuguese striker who had looked for the contact.

Spain began to dominate possession but failed to turn that into chances until the 23rd minute when Costa won a high ball, twisted and turned and then beat Rui Patricio with a low shot.

A minute later Isco hit the bar from 25 yards and Spain were clearly on top, with their mobile midfield quintet causing Portugal problems with their pace and mobility as Iniesta shot just wide in the 35th minute.

However, on the stroke of halftime a dreadful error from Spain keeper David de Gea allowed Ronaldo to put Portugal back in front with a left foot shot from outside of the penalty area, which the keeper should have easily saved.

The second half began with a similar pattern to the first with Spain dominating possession and they were level 10 minutes after the break after Sergio Busquets nodded a Silva free kick across goal for Costa to score his second of the night from close range.

Three minutes later Spain were ahead when Nacho’s volley from outside the area went in off the post and in the 69th minute Thiago Alcantara replaced Iniesta, moments before Costa fired wide from a low Jordi Alba cross as Spain continued to move the ball fluidly.

Portugal substitute Ricardo Quaresma hit a shot over the Spain bar and a run from Ronaldo had De Gea struggling as he came off his line, but Spain looked as if they were going to hold on until Ronaldo produced a moment of magic with an 87th minute free kick which left De Gea rooted to the spot.