Carlos Queiroz’s side looked to have gained a valuable draw and had opportunities to steal a win themselves, notably through a couple of Reza Ghoochannejhad efforts in the second half.
Argentina dominated the majority of the game but their star frontline of Messi, Angel Di Maria, Sergio Aguero and Gonzalo Higuain lacked their usual cutting edge in the final third.
But, when it looked like Iran had grabbed a remarkable point, Barcelona's Messi curled in a trademark effort past a sprawling Alireza Haghighi to guarantee Argentina’s place in the last 16.
Iran had the first chance of the game when Fulham’s Ashkan Dejagah whipped in a superb free-kick onto the head of an unchallenged Jalal Hosseini, who glanced his header wide of Sergio Romero’s goal after just four minutes.
Argentina’s first major opportunity came when Fernando Gago slipped the ball through to an onrushing Higuain, but the Napoli striker hit it straight at goalkeeper Haghighi from six yards out.
Alejandro Sabella’s side continued their pressure but were denied by Haghighi once again when Aguero’s goal-bound effort was palmed away by the Rubin Kazan stopper.
Iran spent most of the first half camped in their own half and were seemingly delaying the inevitable, with Marcos Rojo spurning another Argentine chance after heading wide from six yards.
Continuing to dominate possession, Messi dinked a free-kick towards Federico Fernandez, but it was taken off his head by team-mate Ezequiel Garay who headed it high over the bar.
With just five minutes remaining in the first half, Dejagah’s corner found Hosseini once again and the right-back, who had more shots than he made passes in the first half, was agonisingly close to giving his side a shock lead after heading over the bar.
Queiroz’s side came into the game as 25/1 outsiders, but went into the half-time break with a clean sheet still remarkably intact.
After the restart, Argentina unsurprisingly continued their onslaught on the Iranian backline and, after Messi fed through Pablo Zabaleta, the right-back scooped his effort high and wide of Haghighi’s goal.
Iran then broke on the counter with Pejman Montazeri, who delivered a wonderful ball into the area which was met by Ghoochannejhad, who had broken free of his marker, but the Charlton Athletic striker’s header was stopped comfortably by Romero.
The Middle Eastern side then had a huge shout for a penalty after Dejagah was brought down in the area by Zabaleta, but referee Milorad Mazic ruled that the Manchester City man had got a touch on the ball.
Having spent a large majority of the game absorbing pressure, Iran’s confidence grew as Argentina’s nerves began to show, with Garay almost diverting an Iranian cross into his own net.
Moments later, Montazeri delivered a sumptuous cross into the heart of the Argentine area and Dejagah’s looping, goal-bound header was magnificently tipped over by Romero to spare Sabella’s blushes.
Argentina’s frustrations grew as Messi’s 20-yard free-kick crashed into the side-netting, while Di Maria drilled a close-range shot straight into the grasp of Haghighi.
But, in stoppage time, Messi was given room to manoeuvre and cut in from the right-hand side before curling an unstoppable effort into the far corner to break Iranian hearts, and send the droves of Argentinian fans into delirium.