Iran came agonisingly close to claiming a winning goal, which would have seen them qualify for the next round instead of Portugal, but that went just wide of the goal.
Portugal finished tied on five points with Spain, who drew 2-2 against Morocco to top the group by scoring more goals, and set up a second round clash with Group A toppers Uruguay in Sochi on Saturday.
Making his World Cup debut at the age of 34, Quaresma played a one-two with Adrien Silva to cut in from the right flank and curled the ball past the outstretched hands of goalkeeper Alireza Beiranvand with the outside of his boot.
Iran were awarded a penalty in stoppage time after Paraguayan referee Enrique Caceres used the video assistant referee (VAR) system to rule that Portugal’s Cedric Soares had handled the ball in the box.
Ansarifard converted the penalty to make it 1-1, pushing Portugal to second spot in the group behind Spain.
Portugal were left to rue a missed penalty by Cristiano Ronaldo in the 51st minute after Beiranvand pulled off a heroic save to deny the European champions a 2-0 lead.
Spain, meanwhile, will meet hosts Russia at Moscow’s Luzhniki Stadium on Sunday.
Substitute Iago Aspas scored in stoppage-time to earn Spain a dramatic 2-2 draw with Morocco on Monday which gave them top spot in World Cup Group B and a place in the last 16.
With Morocco heading towards their first win of the tournament, Iago Aspas delivered the late heroics when he back-heeled the ball into the net to cancel out Youssef En-Nesyri’s brilliant 81st-minute header for Morocco.