About 100 current or former heads of state or government gathered at the First National Bank stadium in Soweto, Johannesburg, to pay tribute to Nelson Mandela.

The four-hour memorial service for the former South African president produced a few surprises. It was dominated by a powerful speech by US president Barack Obama.

He told the thousands who braved torrential rain to attend the event that the late leader was "a giant of history".

"We will never see the likes of Nelson Mandela again. But let me say to the people of Africa, and young people around the world: You can make his life's work your own," Obama said.

Before he took the stage Obama shook hands with Cuba's president Raul Castro. The encounter was as brief as it was historic. Obama was only the second US president to shake hands with a Cuban leader since Washington and Havana broke off diplomatic relations in 1961.

Bill Clinton, who was also at the memorial service, came first in 2000, when he shook hands with Raul Castro's brother, Fidel.

Raul Castro addressed the stadium that was not as packed as the authorities expected.

"Let us pay tribute to Nelson Mandela: The ultimate symbol of dignity and unwavering dedication to the revolutionary struggle, to freedom and justice, a prophet of unity, peace and reconciliation," Castro said.

"As Mandela's life teaches us, only the concerted effort of all nations will empower humanity to respond to the enormous challenges that today threatens its very existence," he said.

Most speakers were warmly applauded but there was a cold reception for host president Jacob Zuma, who was repeatedly booed.

Analysts said that many South Africans were unhappy with Zuma because of state corruption scandals.

Notable attendees included UN Secretary-General Ban Ki-moon, David Cameron, Gordon Brown, Tony Blair and Sir John Mayor, French president Francois Hollande and his predecessor and rival, Nicolas Sarkozy, Italy's prime minister Enrico Letta and Brazilian president Dilma Rousseff.