SOCHI, Russia — Cristiano Ronaldo completed a hat trick with a perfectly taken free kick in the 88th minute to give Portugal a 3-3 draw against Spain on Friday at the World Cup.

Ronaldo had twice given Portugal the lead with first-half goals at Fisht Stadium, but Diego Costa equalized for Spain with a goal in each half. Nacho Fernandez then put the 2010 tournament champions ahead with a one-timer from outside the penalty area in the Group B match.

Spain looked to have successfully overcome its dramatic coaching change on the eve of the tournament, but Ronaldo curled a late shot over the wall to force the draw and record his sixth hat trick in international play.

"Against a player like Ronaldo, these things can happen," newly appointed Spain coach Fernando Hierro said. "It's very fortunate for whatever team has Cristiano Ronaldo."

Ronaldo's opening goal from the penalty spot allowed him to become the fourth player to score in four World Cups, joining Miroslav Klose, Pelé and Uwe Seeler. Ronaldo also became the first Portuguese player to appear in four World Cups.

"I've said it so many times: Cristiano is the best in the world," Portugal coach Fernando Santos said.

Said Ronaldo: "To me, the most important is to highlight what the team has done."

His second goal came after a blunder by Spain goalkeeper David de Gea, who let the ball bounce off his hands and into the net.

Spain badly needed a winning start after the federation fired coach Julen Lopetegui two days before its tournament debut for accepting a job with Real Madrid without letting it know in advance. Hierro, a former player, was picked as Lopetegui's replacement and was on the bench despite no significant previous coaching experience.

"It wasn't an easy situation," Hierro said. "When you have this staff and these young players, they make it much easier."

The teams arrived in Russia as favorites to share first and second in the group, but the draw leaves them two points behind Iran, which defeated Morocco 1-0 earlier Friday.

Spain, trying to rebound from disappointing eliminations at the 2014 World Cup and the 2016 European Championship, will next face Iran, while Portugal plays Morocco.

Iran 1, Morocco 0

ST. PETERSBURG, Russia — An own goal in the fifth minute of injury time gifted Iran a win in its opener.

Only on the field as a 77th-minute substitute, Morocco striker Aziz Bouhaddouz dived to reach a curling cross from the left but headed the ball past his own goalkeeper.

While Bouhaddouz buried his face in the turf and was consoled by teammates, there was an explosion of joy among the Iranians as coaches and substitute players sprinted onto the field and jumped for joy.

It was Iran's second win in 13 matches at the World Cup, and Asia's first since the 2010 tournament in South Africa, and it came in what was regarded as a must-win game for the two outsiders in a group with Portugal and Spain.

Uruguay 1, Egypt 0

YEKATERINBURG, Russia — Jose Gimenez came to Uruguay's rescue by scoring the lone goal in its Group A opener.

The defender jumped into the penalty area and headed home captain Diego Godin's free kick late in the 89th minute.

Russia leads Group A on goal difference after beating Saudi Arabia 5-0 Thursday in the tournament's opening match. Uruguay is now second, also with three points.

Uruguay won its opening match at the tournament for the first time since 1970, while Egypt has yet to win a World Cup game.

Egypt forward Mohamed Salah was on the bench for his country's first World Cup match since 1990 after injuring a shoulder while playing for Liverpool in last month's Champions League final. Salah was the Premier League's player of the year after scoring a league-leading 32 goals, with 44 in all competitions.