Messi levelled the scores after 31 minutes after Gonzalo Higuain robbed B razilian captain Sandro and fed the striker with a defense-splitting pass for him to shoot low into the corner of the goal.
Within three minutes, the diminutive striker ran clear again to put Argentina 2-1 up, collecting a pass from Angel di Maria and rounding Brazilian goalkeeper Rafael Cabral.
A young Brazil side, facing a more experienced Argentina, had opened the scoring in the 23rd minute when a quickly taken free-kick found Romulo, who banged in the goal as Argentina's defenders appealed in vain for offside.
After half time, Brazil got back in the game when Oscar scored from close range after 56 minutes and then reclaimed the lead when Hulk took advantage of a fumble by Argentine goalkeeper Sergio Romero to score.
Argentina stormed back quickly in a rollercoaster game as Federico Fernandez headed in from a corner in the 75th minute.
Then, with six minutes of normal time left, Messi scored the best goal of the game with a trademark run and an unstoppable shot into the top corner of the goal from outside the penalty area.
"It's always beautiful to beat Brazil and I am very happy with my three goals," Messi told reporters after the game.
Messi has been criticized in the past for not repeating for the national side his brilliant form with Spanish giants Barcelona that earned him the World Player of the Year title for the last three years.
But Saturday's hat-trick was Messi's second in three matches for Argentina and his tally for the national side now stands at 26 in 70 matches.
Messi thanked his parents for the turnaround. "They more than anyone else know what I suffered in the national team during the bad times I went through. They were always by my side then and now they are having as much fun as me."
Argentina's coach Alejandro Sabella paid tribute to Messi.
"Thankfully for us, he's Argentine and we can enjoy having him in our team," Sabella told a news conference. "He's an exceptional player who is going through an exceptional phase."
Brazilian coach Mano Menezes was left scratching his head about Messi who made the difference in a game in which Brazil created a string of scoring opportunities.
"No one in the world has found a solution (to playing against him)," Menezes said. "He had four chances and scored three goals...Against some players you cannot give any space and perhaps we gave him too much."
Brazil's Marcelo and Argentina's Ezequiel Lavezzi were sent off for an altercation in injury time.
The game was played in front of a sell-out crowd of nearly 82,000 - many of them Brazilians and Argentines living in the United States - at Meadowlands Stadium, home of New York's two American football teams, the Giants and the Jets.
The defeat was Brazil's second in less than a week after they lost 2-0 to Mexico last Sunday.
But coach Menezes said he remained confident about the Olympics.
"We have a very good Olympics team and we will only make a few small changes before we start," he said.
Brazil, who have won the World Cup a record five times, have never won the Olympics where teams are required to field teams mostly made up of players under 23 years of age.