After losing the first set 4-6, Federer broke back against the world number one early in the second set to take a 6-3, 6-3 lead, watched by a capacity home crowd including Formula One champion Sebastian Vettel, ski-ing champions Lindsey Vonn and Didier Cuche.
The 10,500 tickets for the "Match for Africa" were sold out within a few minutes when they went on sale on September 1.
The world's top two in tennis this year move on to Madrid for another exhibtion match on Wednesday, this time to raise money for the Spaniard's charity.
"Both of us realise we are in a very privileged position, where we have everything in life, the least we can do is to help people who are in difficulty," said Nadal.
Federer has been hoping to raise around one million dollars from the event for his foundation to support projects for African children in education and sport.
"You have to look at the long term," said Federer.
"When you can help 1,500 children to go to school, it's great but you need a lot around it: how do the children get to school, what do they eat at school, do they have toilets... and that's where we come in," he added on Swiss television TSR.
The 29 year-old Swiss player remains on the wrong end of an 8-14 career record on the professional circuit against Nadal, breaking a six match losing streak against the Spaniard this year.