Spain, who lost their manager Julen Lopetegui on the eve of the tournament, were seemingly heading towards victory after coming from behind twice to lead 3-2 with just minutes remaining, but Ronaldo, who became the first player to score in eight consecutive major tournaments, denied them right at the death on Friday night.
Earlier, the Real Madrid forward opened his Russia 2018 account from the penalty spot in the fourth minute before Diego Costa fired Spain level in the 24th minute.
David de Gea’s error just before half-time handed Ronaldo (44) his second of the match and Portugal the lead at half-time but after the break, Spain hit back again through Costa (55) before Nacho’s thunderbolt (58) gave his side the lead.
Fernando Hierro, who took over from Lopetegui, was seemingly heading for a winning start to his reign until Ronaldo once again grabbed all the headlines.
The news from the Spain camp had dominated the build-up to this game but Ronaldo shifted the attention back onto him as early as the fourth minute as his stepover drew a foul from Nacho to win a penalty. The Real Madrid striker got up to convert emphatically past De Gea from the spot.
Spain responded well to going behind and they hit back through Costa, who created a yard for himself ahead of Jose Fonte before firing past Rui Patricio and into the bottom corner from the edge of the penalty area.
Minutes later, Isco was inches away from giving Spain the lead. His powerfully struck shot beat Patricio but the ball crashed off the underside of the crossbar and down onto the goal line, with technology showing it had not crossed the line.
Hierro’s side were starting to dominate the ball, with Isco their main threat. The Real Madrid midfielder pulled the ball back for Andres Iniesta but his shot flashed just wide of the far post.
Despite Spain’s dominance, Portugal remained a threat on the counter-attack with Ronaldo leading the line, and the 33-year-old grabbed his second of the game thanks to De Gea’s error. Goncalo Guedes teed up his captain, whose shot was straight at the Manchester United goalkeeper, but Spain’s number one could only help it into the back of the net.
Hierro’s words at the break roused Spain as they regrouped to draw level in the 55th minute. David Silva’s cross was headed back across goal by Sergio Busquets and Costa was on hand to tap in from close range.
Three minutes later and Spain were in front thanks to Nacho’s thumping half-volley as the ball flashed past Patricio and into the net off the post.
Spain were on course for what would have been a morale-boosting win but it was left to Ronaldo to have the final say, as he curled his 88th-minute free-kick over the wall and into the top corner to round off a night which saw Russia 2018 burst into life.