Brazil finished top of Confederations Cup Group A by beating Italy in their final pool game in Salvador, while Mexico went home on a winning note against Japan.
Barcelona new boy Neymar maintained his record of scoring in each Brazil match at the tournament while Fred again showed his goalscoring ability, scoring twice in a 4-2 win over Italy.
Neymar grabbed his team's second goal as the hosts forged ahead for a second time after Dante's opener had been cancelled out by Emanuele Giaccherini.
Fred then netted twice, with Giorgio Chiellini on target in between, as the hosts extended their perfect start to the competition to head into the knockout stage - alongside the Azzurri - full of momentum.
Both teams headed into the game having already assured their places in the semi-finals, having beaten Mexico and Japan in their previous outings, and were effectively playing for the right to avoid in-form world and European champions Spain in the next round. Italy are now likely to face that unenviable task.
Javier Hernandez scored twice as well as missing a penalty as Mexico beat Japan 2-1 in Belo Horizonte to finish third.
The Manchester United striker headed home twice in the second half after Mexico recovered from a miserable first 45 minutes when they produced nothing of note in front of goal.
Japan pulled a goal back through Shinji Okazaki with five minutes left but Hernandez squandered the chance of claiming a hat-trick when his injury-time penalty was saved and he managed to miss the rebound from close range.
It leaves Mexico third in the group with three points but Japan leave Brazil without a point after three defeats.