Superstars Roger Federer and Rafael Nadal set a new attendance record for a tennis match, as more than 51,000 spectators turned up to watch the longtime rivals play a friendly exhibition in South Africa.

A sellout crowd of 51,954 were in attendance in Cape Town Stadium on Friday evening for the highly anticipated Match in Africa, which Federer won 6-4, 3-6, 6-3.

More:

The exhibition raised $3.5m for the Roger Federer Foundation, which supports early childhood education in six southern African countries, including South Africa, the home country of Federer's mother.

"It was an absolute pleasure to share the court with Rafa (Nadal) again, but the first time here in beautiful Cape Town in South Africa. It means so much more and ... it really is very, very special on so many levels," the Swiss player said after the match.

Friday's crowd - the biggest ever on record for a tennis match - broke the previous record of 42,517 set in November when Federer played Germany's Alexander Zeverev in another exhibition in Mexico City.

Organisers said the demand for the Federer-Nadal match was in the region of 200,000 ticket requests.

"We tried our best as always," Nadal, world number two, said. "It's a lifetime experience playing in front of such an amazing crowd in an amazing stadium. It was a big pleasure for me to be part of it."

Federer pulled in some big-name friends alongside the Spaniard to help out on the night: Microsoft co-founder Bill Gates and The Daily Show host Trevor Noah.

Before the main match, Federer and Gates teamed up to beat Nadal and Noah 6-3 in a one-set doubles exhibition.

Comedian Noah did plenty of good-natured trash talking. He shares a similar heritage to Federer. Both have Swiss fathers and South African mothers.

"Roger's at a disadvantage. We're both half-Swiss, half-South African, so I'm in his head. I know what he’s going to do," Noah, who wore a pink shirt and black headband to match partner Nadal, said before the doubles match.

Nadal has a 14-10 advantage over Federer in finals, leads 10-4 in their meetings at Grand Slams, and 24-16 in their career matches. Five years younger, the Spaniard may well go past Federer's record of 20 Grand Slams.

"I hope I can still play for a bit longer," world number three Federer said. "We'll see how much more. But there's still lots to look forward to."