Shoja Khalilzadeh turned home after a goalmouth scramble but his effort was disallowed following a lengthy video assistant referee check, while Saeid Ezatolahi headed against the woodwork in the seventh minute of added time.
Wild celebrations had followed Khalilzadeh putting the ball in the net, with the defender removing his shirt - for which he was later booked - and posing for a photograph with a pair of sunglasses.
His, and Iran's, joy was short-lived, though.
Iran, who have drawn all three of their group matches, remain in with a chance of reaching the World Cup knockout phase for the first time ever as a best third-placed qualifier.
Their hopes now rest on the outcomes of Groups J, K and L, while Egypt finished second in Group G.
Results in other groups earlier in the day had already confirmed Egypt's progression to the last 32 before kick-off, although it was possible for them to finish first, second or third in the group.
Belgium went through as group winners, courtesy of their victory against New Zealand.
In an eventful start, Egypt took the lead in the fifth minute when Mahmoud Saber shot through a crowd of players to open the scoring.
Iran produced a fine response and although Mehdi Taremi's penalty was saved by Mostafa Shobeir, they found an equaliser shortly afterwards when Ramin Rezaeian smashed in a rebound from a tight angle.
Captain Mohamed Salah, needing one more goal to equal his head coach Hossam Hassan's all-time scoring record for Egypt, was substituted after 57 minutes.
Before the dramatic finale, Taremi had headed against the bar from a corner in the closing stages, although the referee blew for a foul so it may not have counted had he found the net.
Iran have been competing at the World Cup amid the backdrop of the country's conflict with the United States.
The Iran team were scheduled to be based in Arizona during the tournament, but switched to a new base in Tijuana, Mexico before it began.
They have also faced travel restrictions during their campaign.
For their first two matches in Los Angeles, Iran's squad was only allowed to enter the United States the day before the match and had to leave again on the same day as the game, under the terms of their visas.
A US Department of Homeland Security statement said Iran's players were "permitted to come into the US two days before the match" against Egypt, but would "still be required to leave the day the match ends".
What do Iran need?
Iran will find out later on Saturday if they have done enough to progress as one of the eight best third-placed teams.
To go through, they require just one of the following scenarios to occur:
- Croatia to lose to Ghana in Group L
- DR Congo to fail to beat Uzbekistan in Group K, although Uzbekistan must not win by a seven-goal margin or better
- Either Algeria or Austria to win their meeting in Group J
Meanwhile, as Group G runners-up, Egypt will play Australia in their last-32 tie in Dallas on Friday, July 3.