Barack Obama shook hands with Cuba's president, Raúl Castro, at a memorial service for Nelson Mandela.

Obama was greeting a line of world leaders and heads of state attending the memorial in Johannesburg. He also shook hands with the Brazilian president, Dilma Rousseff, who has clashed with Obama over National Security Agency spying.

The US and Cuba have recently taken small steps toward rapprochement, raising hopes that the two nations could be on the verge of a breakthrough in relations. But sceptics caution that there have been signs of a thaw in the past, only for the cold war enemies to fall back into old recriminations.

In his speech to the memorial service, Obama praised Mandela as the last great liberator of the 20th century, urging the world to carry on his legacy by fighting inequality, poverty and discrimination.

Obama compared the former South African president to Mahatma Gandhi, Martin Luther King Jr and Abraham Lincoln.

"For nothing he achieved was inevitable," Obama said. "In the arc of his life, we see a man who earned his place in history through struggle and shrewdness, persistence and faith. He tells us what's possible not just in the pages of dusty history books, but in our own lives as well."

The crowd at the half-filled stadium erupted in applause each time Obama's name was mentioned or his image was shown on the screen. Dozens gathered below the box seats where Obama and other American presidents sat, waving and snapping pictures of the leaders.

Calling himself a beneficiary of Mandela's struggle, Obama traced the influence that Mandela's story has had on his own life, disclosing that he asks himself how well he's applied Mandela's lessons to himself as a man and as president.

He said in the US, South Africa and around the world, people must not allow progress that's been made to cloud the fact that more work must be done.

"We, too, must act on behalf of justice. We, too, must act on behalf of peace. There are too many of us who happily embrace Madiba's legacy of racial reconciliation, but passionately resist even modest reforms that would challenge chronic poverty and growing inequality," Obama said, referring to Mandela by his traditional clan name.

Extolling Mandela as practical but unyielding on his core principles, Obama said it was because Mandela could admit to being imperfect that the world loved him and continues to learn so much from his example. "He was not a bust made of marble. He was a man of flesh and blood," Obama said.

He said Mandela had changed both laws and hearts, inspiring those around him by reconciling with the jailers who kept him prisoner for 27 years. In trusting others despite the injustices he suffered, Mandela showed that the cruelty of the past must be confronted with truth, generosity and inclusion, Obama said.

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

Joining Obama on the 16-hour trip from Washington for the ceremony were first lady Michelle Obama, former President George W Bush and his wife, Laura, and former secretary of state Hillary Clinton. Former presidents Bill Clinton and Jimmy Carter also attended the memorial service.