The result has seen Argentina reach the last 16 of the tournament with one game left to play in Group F.
© Getty Images
Both sides enjoyed an even start to the match, but it was Iran who almost made the breakthrough with just four minutes played when Jalal Hosseini headed inches wide.
Argentina threatened again on 21 minutes when Sergio Aguero was allowed to get a shot off inside the penalty area, only for goalkeeper Alireza Haghighi to push the ball away from danger in acrobatic style.
The chances continued to fall the way of the dominant South Americans as Messi curled a free kick over the crossbar, while Ezequiel Garay somehow headed over from close range moments later after getting on the end of another Messi free kick.
A final opportunity of the first half went to Iran when Hosseini rose highest once more in the box, though he could only head over on this occasion.
© Getty Images
The Iranians continued to frustrate their opponents into the second half, though they had reason to be worried when Messi ran at their defence menacingly with just under an hour played. However, his dynamic break ended with a wayward shot.
Iran, who sat deeper and deeper as the match progressed, carved open a wonderful chance on 66 minutes as Ashkan Dejagah got in front of his marker to get a header on goal, but Sergio Romero managed to tip it over the bar with the deftest of touches.
Argentina threw everything at their opponents as the clock ticked down towards the 90-minute mark, with Rodrigo Palacio and Ezequiel Lavezzi brought on to liven up their attack, but the Iranians held firm.
Iran could have snatched a dramatic win in the final five minutes as Reza Ghoochannejhad was sent clear through on the Argentina goal, but his effort on the angle was pushed away by Romero.
However, with the first minute of injury time played, Messi was allowed to cut onto his left foot inside the area before drilling an unstoppable shot into the top corner to claim Argentina a dramatic triumph.