Gonçalo Ramos scored a stoppage time winner to send Portugal past Croatia into a last 16 showdown with Spain.
The goal should have ended a compelling game at the Toronto Stadium but it launched a high-voltage climax in which Josko Gvardiol's strike was ruled out for offside following a review by the video assistant referees.
The frenzied finale contrasted with a sedate first-half.
Following the pause, a more vigorous Croatia emerged.
And the veteran striker Ivan Perisic gave them the lead their dominance deserved.
Ronaldo's first goal in knockout stages
But they threw away that advantage 15 minutes later. Nikola Vlasic dragged Portugal defender Renato Veiga down in the penalty area as they tried to compete for the ball from a corner kick.
Referee Espen Eskas consulted the video monitors at the side of the pitch and awarded a penalty.
Portugal skipper Cristiano Ronaldo stepped up and rammed the ball past the goalkeeper Dominik Livaković to register his first goal in a knockout round at a World Cup.
Ramos, who will turn out for AC Milan at the start of next season after three years at Paris Saint-Germain, took the plaudits.
On Monday in Dallas, Portugal will face Spain who swept past Austria 3-0 in Los Angeles on Thursday. Mikel Oyarzabal scored twice and Pedro Porro got the other goal.