Portugal qualified for the round of 16 at the World Cup despite being held 1-1 by Iran in World Cup Group B on Monday after Cristiano Ronaldo missed a penalty.
The European champions, needing a draw to qualify, went ahead with a stunning Ricardo Quaresma goal in the 45th minute.
Ronaldo had a chance to extend the lead when he won a penalty in the 51st minute but Iran goalkeeper Alireza Beiranvand dived to his left to save his effort from the spot.
Karim Ansarifard equalized with a stoppage time penalty awarded after a video review. However, Portugal clung on to finish second in the group with five points behind Spain, who drew 2-2 with Morocco, on goals scored.
The results from Monday's late matches mean Spain will play Russia, and Portugal will meet Uruguay to begin the round of 16.
Iran and Morocco have been eliminated.
Iran were well organized at the back and defended stoically but did not have enough men going forward.
Carlos Queiroz's men relied mostly on counter attacks and while forward Sardar Azmoun showed plenty of glimpses of his talent, it was not enough to deny the Portuguese.
"It's such a big disappointment for us," Iran midfielder Alireza Jahanbakhsh said. "We had a big chance at the end of the game, we could have scored and won it. As everyone has seen, we did our best to win this game but we missed some chances."
Portugal had faced a lot of criticism for their lackluster display and inability to hold possession in their 1-0 win against Morocco last Wednesday but they appeared determined to set that right against the Asian powerhouse.
They dominated possession, playing almost three times the number of passes than their Asian opponents, and were more intent on creating chances through the midfield rather than just directing long balls towards Ronaldo.