Mexico had come from behind three times to send the game into extra time at 3-3 but in the 97th minute Ballack swerved a free kick from the edge of the area past Oswaldo Sanchez.
Germany were relieved after throwing away the lead three times, and it could have been four had Luis Perez not blazed in the final minute.
petulance
Striker Mike Hanke was sent off in the 54th minute for a late tackle.
In what was supposed to be a friendly match Hanke clattered into Carlos Salcido with the ball long gone and received a straight red card for his uncalled-for petulance.
At the time of the incident Germany led 2-1 thanks to goals from Lukas Podolski (37min) and Bastian Schweinsteiger (41) but Mexico equalized again through Jared Borgetti after 58 minutes.
Robert Huth then put Germany in front for the third time in the 79th minute but Borgetti equalized with a bullet header right on target six minutes later.
But it was Ballack who had the final say, scoring his fourth goal in this competition -- the other three have been penalty kicks.
Mexico, who were exhausted after playing 120 minutes in the semifinal defeat against Argentina, will rue some defensive lapses here, but can take heart from a 1-0 win over Brazil in the group stage.
Germany, who lost the 2002 World Cup final 2-0 to Brazil, have produced some cavalier entertaining football in this competition but the number of goals conceded is a concern ahead of the 2006 World Cup.
In five Confederations Cup games the hosts have shipped in 11 goals and goalkeeper Oliver Kahn, celebrating his 80th cap, must wonder what is going on.
In his two matches at this competition he has conceded six goals, with Australia scoring three in their 4-3 defeat.



