Iran were incredibly awarded a penalty of their own in the 93rd minute following intervention from VAR, however, and Karim Ansarifard converted from the spot to set up a grandstand finish on Monday night.
Portugal looked to have won the group entering the final moments, but Iran's equaliser and a leveller for Spain against Morocco means that Santos's side will take on Uruguay in the round of 16 on June 30, leaving Spain to face tournament hosts Russia on July 1.
Ronaldo had the game's first attempt in the third minute, and the Real Madrid attacker brought a smart save from Iran goalkeeper Alireza Beiranvand, before Vahid Amiri fired wide of the post down the other end four minutes later.
Portugal had another opportunity in the ninth minute following a mistake from Beiranvand, but Joao Mario could only lift the ball over the crossbar as chances continued to arrive.
Beiranvand made another mess of a Quaresma cross in the 14th minute as Portugal continued to threaten, but Iran managed to clear their lines.
Saeid Ezatolahi flicked a header into the arms of Portugal goalkeeper Rui Patricio 10 minutes before the break as Iran threatened once again, before Ronaldo fired into the body of Beiranvand down the other end of the field.
Portugal did take the lead in the final stages of the first half, however, with Quaresma using the outside of his boot to sensationally bend the ball into the top corner of the net.
There was a hugely controversial moment early in the second period when Portugal were awarded a penalty following a VAR review, with Ronaldo going down inside the box under a challenge from Ezatolahi.
The Madrid forward stepped forward looking to score his fifth goal of the 2018 World Cup, but Iran goalkeeper Beiranvand kept out Ronaldo's spot kick with a smart save.
Temperatures were rising in Saransk as late challenges continued to arrive, and Quaresma was a touch fortunate to escape with only a yellow card for a high challenge on Ezatolahi.
Adrien Silva then fired a wild effort over the Iran crossbar 20 minutes from time, before Saman Ghoddos just missed the Portugal post with a powerful strike moments later.
VAR was again called into action 10 minutes from time, and Ronaldo picked up a yellow card for a collision with Morteza Pouraliganji, but the Iranian players wanted a red.
Iran were incredibly awarded a penalty in stoppage time when VAR asked the referee to have a look at a potential Cedric Soares handball, and Queiroz's side converted from the spot to set up a grandstand finish.
Iran had a golden chance to win it - and book their spot in the round of 16 as a result - in the final moments, but Mehdi Taremi somehow fired wide of the post from close range as Portugal advanced following a quite incredible second half of football.
IRAN (4-5-1): Beiranvand; Hajsafi (Mohammadi 55'), Pouraliganji, Hosseini, Ramin; Taremi, Omid, Ezatolahi (Ansarifard 76'), Amiri, Jahanbakhsh (Ghoddos 70'); Azmoun
PORTUGAL (4-4-2): Patricio; Cedric, Pepe, Fonte, Raphael; Mario (Moutinho 84'), W Carvalho, Adrien Silva, Quaresma (Bernardo Silva 70'); Andre Silva (Guedes 90'), Ronaldo