"We were so thankful to be playing in front of so many Brazilians, especially playing away from home," Kaka said.
Kaka a 1-1 tie in the 80th minute after Rafael Marquez's failed to clear a cross.
Juan Carlos Cacho gave Mexico a lead in the 43rd minute, but two minutes later, Ronaldinho, who gave another brilliant performance, provided the corner kick that Kleber struck home with his left foot.
Afonso sealed the win in the 86th minute.
Referee Baldomero Toledo ejected Brazilian Elano and Brazil coach Dunga.
The crowd 61,316 of at the 2002 MLS Cup -- Los Angeles' 1-0 win over the New England Revolution - was the previous largest soccer crowd at Gillette Stadium.