Just three days after they recovered from being 2-0 down to beat France 3-2 in the semi-finals, Brazil scored two goals in the last 10 minutes to overcome their Latin rivals.
Although Brazil had more of the early chances, Mexico took the lead at 66 minutes when Bryan Gonzalez rose between two defenders to head a cross from the left into the net.
Kaio Jorge equalised from the penalty spot with six minutes remaining after the Video Assistant Referee (VAR) advised officials to pull play back following a challenge on Veron.
Then three minutes into stoppage time, substitute Lazaro ran on to a cross ball and calmly fired home, to the delight of the home crowd.
The Flamengo forward had also come off the bench to score the winner in Thursday’s last-minute win over France.
“They use the VAR when it is convenient for them,” said an unhappy Mexican coach Marco Ruiz. “We had the game under control until the VAR decision. They grew after that.”
The win was revenge of a sorts for Brazil, who lost 3-0 to Mexico in the under-17 final in 2005, as well as to their under-23 team in the final of the 2012 Olympics.
Earlier in the day, Arnaud Kalimuendo-Muinga scored a hat trick to help France come from behind to beat the Netherlands 3-1 and take third-place.
Dutchman Sontje Hansen won the golden boot for the top goalscorer with six goals.