JOHANNESBURG (AP) — Celebrating one of his personal heroes, President Barack Obama praised Nelson Mandela as the last great liberator of the 20th century, urging the world to carry on his legacy by fighting inequality, poverty and discrimination.

At a memorial service in Johannesburg, Obama compared the former South African President to Mahatma Gandhi, Martin Luther King Jr. and Abraham Lincoln.

"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 U.S. presidents sat, waving and snapping pictures of the leaders.

As if to underscore the spirit of reconciliation that Mandela's life embodied, Obama shook hands with Cuban President Raul Castro as he made his way down a line of world leaders gathered to honor the anti-apartheid leader. It was a rare moment of accord for the leaders of the two Cold War enemies. Later, aboard Air Force One, deputy national security adviser Ben Rhodes said the exchange was not planned and did not signal a change in policy.

"We recognize that it's been quite some time since the presidents of the United States and Cuba were even in the same place," Rhodes acknowledged.

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 U.S., 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 Rodham Clinton. Former Presidents Bill Clinton and Jimmy Carter also attended the memorial service.

During the memorial service, Obama also had time to greet President Hamid Karzai of Afghanistan, though the White House said the two men did not discuss the status of a security agreement between Afghanistan and the United States. Others whom Obama greeted were French President Francois Hollande, Kenyan President Uhuru Kenyatta, and Brazilian President Dilma Rousseff.

The White House said Obama did meet with members of Mandela's family during the memorial service, including Mandela's widow, Graca Machel, and expressed condolences on behalf of the American people.Obama and first lady Michelle Obama also called Archbishop Desmond Tutu of South Africa from their hotel after the service to thank the retired Anglican bishop for his anti-apartheid work.