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.

People sing and dance in heavy rain in Soweto, JohannesburgReuters
US president Barack Obama greets Cuban president Raul Castro before giving his speech, as their Brazilian counterpart, Dilma Rousseff, looks onReuters
Former French president Nicolas Sarkozy, his successor Francois Hollande and Italian prime minister Enrico LettaReuters
US president Barack Obama arrives to speak at the memorial serviceReuters
Former US president George W Bush talks to former British prime minister Gordon BrownReuters
Queen Rania of Jordan arrives at the stadium for Mandela's memorial serviceReuters
Britain's prime minister David Cameron attends the memorial service for Nelson MandelaReuters
Obama shares a moment with Denmark's prime minister Helle Thorning-Schmidt as his wife Michelle looks onReuters
Former South African president Nelson Mandela's ex-wife Winnie and current wife Graca MachaelReuters
Mandla Mandela, eldest grandson of former South African president Nelson MandelaReuters
A man waves a South African flag at the First National Bank (FNB) StadiumReuters
In pouring rain, crowds peer intently at the homages to Mandela on a giant screenReuters
President Jacob Zuma holds the official programme but his presence did not go down wellReuters
Princess Charlene of Monaco arrives at the First National Bank Stadium, JohannesburgReuters