The North American country did it with a team few believed would be contending at the end, a team which stunned the heavy favourite Brazilians with a goal just 29 seconds into the final — the fastest Olympic goal ever recorded.
While the Mexicans are celebrating their first gold, Brazil is left trying to regroup and will have to wait another four years to continue its quest for the elusive medal.
But Brazil wasn't the only top contender to fail at this year's football tournament.
Spain didn't even get close to getting its second gold after failing to get past the group stage, just like Uruguay, and Britain's first team since 1960 — without David Beckham — couldn't live up to the expectations from home fans and fell in the quarterfinals.
The men's tournament ended with Mexico beating Brazil 2-1 in an exciting final at Wembley Stadium. The last day also was marked by a controversy that kept a South Korean player from receiving his bronze medal for showing a sign with a political message, which is prohibited by the IOC and FIFA.
Mexico arrived at the London Games with an outside shot at the title as Brazil, Spain, Uruguay and Britain looked set to be fighting for the gold. But with a squad that has been playing together for a few years, it performed well throughout the tournament and showed the Mexico national team can look forward to a promising future.
"It's a very important moment for Mexican football," coach Luis Fernando Tena said. "It's a great moment for us. Our youngsters have developed great mental strength and our football is improving considerably."
Mexico came without one of its top players, Javier "Chicharito" Hernandez, who was not released by club team Manchester United. In the final, it was also without its other main forward, Giovani Dos Santos, because of an injury.
The odds were against Mexico as it faced a Brazilian team which dominated the competition with players such as Neymar, Oscar and Leandro Damiao.
But it was a relatively unknown striker who never played outside of Mexico who gave the nation one of its biggest victories in football. Oribe Peralta scored the early goal and added another in the 75th minute at Wembley to seal Mexico's gold. His first goal was the fastest at least since the 1976 Games, when FIFA began keeping records of the Olympic tournament.
Peralta was one of the three players older than 23 summoned by Tena after Hernandez was ruled out. The 28-year-old striker ended the competition with four goals and quickly helped the Mexicans forget Chicharito's absence.
"We want the people in Mexico to believe in this young generation of players," Dos Santos said. "Today we showed everyone that we are ready to win important competitions. This gold medal goes to all of those in Mexico who always believed in us. I want them to enjoy and to celebrate. For those who didn't believe in us, well, let them celebrate too."
The Brazilians are not celebrating and will not be forgetting these Olympics any time soon. In their third final and with most of its top players, it looked set to finally win the first gold, which is the only trophy the five-time world champions haven't won in football.
"We tried hard, but it wasn't enough," Neymar said. "Sometimes things don't go your way, it happens."
Neymar is touted as the future of Brazilian football and arrived as the tournament's biggest star. He played well but couldn't come up with the outstanding performance that Brazilians hoped he would in the final.
Winning the Olympics was Brazil's priority this year and served as a test for the players, who will likely remain in the team which will try to give Brazil the 2014 World Cup title at home.
Spain's under-23 squad also had high hopes after winning the under-21 European Championship last year. It had some of the players that helped the senior team win its second consecutive Euros a few months ago.
But it was a disastrous campaign in London.
It was eliminated in the group stage after losses to Japan and Honduras and a draw with Morocco. The Spaniards finished last in the group with only one point and didn't score any goals. The result led Spain's football federation not to renew the contract of under-21 coach Luis Milla.
Uruguay also was one of the favourites for the gold after bringing star striker Luis Suarez and other top players, but it followed in the footsteps of Spain and failed to advance past the group stage. It beat the United Arab Emirates but lost to Senegal and Britain.
The hosts were playing without Beckham to the disappointment of fans. Coach Stuart Pearce instead picked veteran Ryan Giggs for the team put together for the first time since the 1960 Games, but it was not enough as it lost to South Korea in a penalty shootout in the quarterfinals.
The defeat also signalled the last appearance for now of a unified British football team, which only played this time because Britain is hosting the event.
Follow Tales Azzoni at http://twitter.com/tazzoniSuggest a correction