Angel Di Maria's strike in the 118th minute after a surging counter-attack launched by Messi settled a gritty duel for Argentina as penalties loomed at Sao Paulo's Corinthians Arena.
The result was cruel on Switzerland, who looked to have done enough to take the game to a penalty shoot-out with a disciplined defensive display that Argentina rarely looked like threatening.
But with two minutes left, Swiss full back Stephan Lichtsteiner was caught in possession near halfway.
Messi accelerated clear, and after hurdling Fabian Schar's attempt to bring him down, slipped a pass to Di Maria who swept the winner past Diego Benaglio.
Switzerland almost hit back with a dramatic equalizer but Blerim Dzemaili's downward header cannoned off the foot of the upright.
Messi's decisive contribution to the winning goal was enough to earn him a fourth consecutive man-of-the-match award in four games at the World Cup.
Argentina will now meet the winner of the final second round between Belgium and the United States.