Oribe Peralta stunned the Brazilians by scoring as some fans were still making it to their seats for the final at Wembley Stadium on Saturday. The heavily favoured South Americans never recovered, losing 2-1 after a late comeback attempt came up just short.
"I don't know if this was the best match of my career, but what I do know is that this is the most important, because I am here today with a gold medal," Peralta said. "I dreamed about this moment. It is one of those things you don't get to live every day."
The surprising result left Mexico celebrating its first Olympic gold in men's football — and Brazil wondering if it will ever add the elusive title to its long list of triumphs in football.
"Yet again we came close, but didn't quite get it," Brazil coach Mano Menezes said.
Peralta took advantage of a mistake by the Brazilian defence in the opening minute and slotted a low right-foot shot just inside the near post. It was the fastest Olympic goal since FIFA began keeping record of the competition in 1976.
The striker added the second with a firm header from a free kick in the 75th.
Hulk scored for Brazil in injury time, but Oscar missed a header in the final seconds to waste the last chance for a comeback.
"This is a great emotion for all our country, Mexico will be celebrating on the streets," coach Luis Fernando Tena said. "It is a great honour for a coach to see his players singing the national anthem with gold medals around their necks. It's a very important moment for Mexican football. It's a great moment for us."
The Olympic gold is the only significant trophy that Brazil, five-time world champions, hasn't won in football. The Brazilians established the London Games as the team's priority this year and arrived as the heavy favourite after bringing most of its top players for the competition. Many will also be in the team for the next World Cup, which will be played in Brazil.
"We're very sorry yet again not to get gold," Menezes said. "Defeat is part of the maturing process. This group will continue on this path and get ready for 2014."
Fans threw straw sombreros in the air and waved Mexican flags at the final whistle. Peralta got hold of one of the wide-brim hats and passed it around among his teammates.
As Mexican players jumped up and down at midfield in celebration, the Brazilians dropped to the ground in despair. Neymar, touted as the future of Brazilian football, was one of them, sitting stoned-faced. Real Madrid left back Marcelo dropped his head and cried as his teammates came to try to console him.
"We are all sad, we know it was probably our only chance to win a gold medal," Neymar said. "Four years from now most of us likely won't be here again so this was our last Olympics. It was our last chance to win the gold."
The Mexicans started celebrating early in front of a crowd of more than 86,000.
Peralta's goal came after Manchester United right back Rafael tried to make a pass backward. Mexican midfielder Javier Aquino intercepted the ball, sending it forward to Peralta just outside the area. With no defenders near him, he calmly fired his shot just inside the near post.
Peralta's second goal came from a free kick taken by Marco Fabian. Peralta broke away from Hulk and ran unmarked in a semicircle behind other Brazilian defenders to power in a header from near the penalty spot. Menezes complained about the foul call that led up to the second goal by the Mexicans.
Mexico could have scored after another mistake by the Brazilian defenders in the 64th. Captain Thiago Silva lost the ball in front of the goal and Marco Fabian sent an overhead kick onto the crossbar with the goalkeeper beaten.
The Mexicans also had a goal by Peralta disallowed for offside in the 69th, and Fabian missed the open net with his header from a corner in the 72nd.
Mexico's previous best Olympic showing was at the 1968 Mexico City Games, when it lost to Bulgaria in the semifinals and then fell to Japan in the bronze match.
Brazil appeared shaken by the early goal and struggled to create scoring opportunities to equalize. Neymar started playing better in the second half and helped Brazil push forward, but struggled to get his efforts on target.
Hulk scored the team's only goal with a low shot from inside the area in the first minute of injury time. Oscar, who just joined Chelsea, had the chance to equalize in the final moments, but his header from inside the six-yard box sailed just over the crossbar.
FIFA said Peralta's goal was the fastest at least since the 1976 Olympics, when football's governing body began keeping record of the competition. The fastest goal in a senior competition was scored by Turkey's Hakan Sukur 11 seconds into a match in the 2002 World Cup in South Korea and Japan.
FIFA said the fastest goal in all competitions was scored by Brazil's Fabinho after nine seconds in the 2007 Under-17 World Cup in South Korea.
Mexico won despite playing without Giovani Dos Santos, one of the team's biggest stars. He was ruled out of the final because of a right hamstring injury.
It was Brazil's first Olympic final in men's football since the 1988 Seoul Games, when youngsters Romario and Bebeto ended with the silver. Brazil also lost the final four years earlier at the Los Angeles Games.
Brazil had to settle for the bronze in Beijing, when a team led by Ronaldinho lost to Lionel Messi and Argentina in the semifinals. The Brazilians also left with the bronze in 1996 in Atlanta with a team that included Ronaldo, Rivaldo, Roberto Carlos and Bebeto.
Follow Tales Azzoni at http://twitter.com/tazzoniSuggest a correction