Mexico goalkeeper Guillermo Ochoa was in inspired form to keep Brazil at bay for long periods, but Brazil star Neymar tapped home a low Willian cross six minutes after half-time to put his side in the driving seat.
As Mexico chased the game in the final minutes, Neymar broke on the left and saw a low toe-poke saved by Ochoa - but Liverpool forward Firmino was on hand to finish into an empty net to seal the game.
Mexico had the better of the first quarter as Brazil struggled to find their rhythm and looked susceptible to counter-attacks.
Neymar's sixth-minute drive was palmed away by Ochoa but that was Brazil's only sight of goal in the opening exchanges.
Carlos Vela and Hirving Lozano were a constant threat to Brazil and the latter's dangerous cross only just eluded the unmarked Javier Hernandez.
Brazil improved as the half wore on but the first period ended goalless.
Neymar tricked his way past Edison Alvarez for Ochoa to block and the goalkeeper had to be alert again to stop Gabriel Jesus' low effort.
In the end it was the Mexicans who were pleased to hear the half-time whistle as Brazil finished the opening 45 minutes on top.
Neymar's second goal of the tournament gave Brazil a 51st-minute lead.
Willian took Neymar's clever back heel to round Ayala and Brazil's talisman continued into the box to slide home the Chelsea man's cross.
Ochoa was in sparkling form with super saves stopping Paulinho and Willian from extending Brazil's lead.
Brazil, though, could not rest easy as Alisson tipped over Vela's drive and the South Americans suffered a blow when Casemiro was cautioned.
It was the influential midfielder's second yellow card of the tournament, ruling him out of a potential quarter-final clash against either Belgium or Japan.
Mexico toiled as they searched for a route back into the game, but lacked any real inspiration in the final third as Firmino wrapped the game up with two minutes left on the clock.
Signup for FREE news updates, latest information and hottest gists everyday/Advertise on NigerianEye.com