Media playback is not supported on this device
Oribe Peralta struck from the 18-yard box after 32 seconds following some poor Brazilian defending.
Fabian Marco hit the Brazil crossbar before a completely unmarked Peralta headed his team's second goal.
Brazil, also hoping to win gold for the first time, pulled a goal back through Hulk in injury-time, before Oscar headed wide when well positioned.
"No one predicted that. I didn't think Mexico would win, let alone be as convincing as that. Full credit to Mexico, they fully deserve their victory."
In pictures: Mexico celebrates
But the South American side scarcely deserved to take the game into extra-time after a very patchy performance, particularly in defence.
Mano Menezes's team had scored three in each of their previous five games and were treating the tournament as an important staging post ahead of hosting the World Cup in 2014.
But if they are to succeed on home soil they will have to show a significant improvement from what they produced at Wembley, with star forward Neymar a largely peripheral figure.
Menezes went into Saturday's match under huge pressure to deliver gold and fill the one significant missing blank for the Brazilian team, but if the game was to be Brazil's coronation as Olympic champions then Mexico clearly had not read the script.
Media playback is not supported on this device
They defeated Brazil 2-0 in a friendly earlier in the summer and stunned Wembley when Peralta's low strike nestled in the bottom corner while many inside the stadium were still taking their seats.
Manchester United defender Rafael was partly at fault for the goal, his sloppy pass allowing Javier Aquino to nip in and dispossess Sandro, with the ball running invitingly into the path of Peralta.
Brazil could not find their stride - a situation not helped by a series of niggly fouls that broke up play and angered coach Menezes, who could be seen waving an imaginary card on the touchline.
And their disappointing start was put into stark perspective when Menezes made a change just after the half-hour mark, bringing on Hulk for Alex Sandro.
"Mexico deserve it, Brazil were arrogant and sloppy. They just thought they could turn up and win. It was a shocking performance from Brazil. They started poorly and continued in that vein."
The substitution made a difference and Jose Corona managed to palm clear a swerving strike from Hulk while Marcelo shot wastefully wide after he had linked with Oscar and Leandro Damiao to carve open the left side of the Mexican defence.
By the early stages of the second half it was obvious that Mexico had opted to try to defend their lead.
This seemed to play into the hands of their opponents, particularly Neymar, who had disappointed in the opening half but briefly relished the chance to repeatedly run at the Mexico defence.
He twice shot wide and saw another effort blocked, but his influence soon faded and Mexico almost struck with a swift break.
There was more shoddy Brazilian defending involved too, as Fabian dispossessed an opponent far too easily and eventually saw his overhead effort rebound off the crossbar.
An unmarked Peralta later slotted home from six yards but Brazil were saved by the offside flag. There was to be no reprieve with 15 minutes remaining.
A free-kick was delivered from the right and the Mexico striker was left completely alone to head home from eight yards.
Brazil's frustrating afternoon saw team-mates Juan Jesus and Rafael square up to each other in the final minutes.
Hulk's injury-time strike into the bottom corner briefly ignited hope of a spectacular comeback and Oscar then headed wastefully wide at the near post as Mexico held on.
Mexico stunned favourites Brazil 2-1 at Wembley to win the men's Olympic football gold medal for the first time.