U.S. President Barack Obama meets with Nkosazana Dlamini Zuma, chairwoman of the African Union Commission, in Johannesburg, South Africa, on Saturday. The visit comes at a poignant time, with former South African president and anti-apartheid hero Nelson Mandela ailing in a Johannesburg hospital.

JOHANNESBURG — President Barack Obama encouraged leaders in Africa and around the world Saturday to follow former South African President Nelson Mandela’s example of country before self.

He and first lady Michelle Obama also Saturday paid personal respects to relatives who have been gathered around the critically ill anti-apartheid icon.

“We as leaders occupy these spaces temporarily and we don’t get so deluded that we think the fate of our country doesn’t depend on how long we stay in office,” Obama said.

Obama spoke at a news conference with South African President Jacob Zuma in the midst of a weeklong tour of the continent that also included stops in Senegal and Tanzania. But many other African nations are embroiled in religious, sectarian and other conflicts.

Obama decided to avoid stopping in his father’s home nation of Kenya because of international disputes there. The International Criminal Court is prosecuting Kenyan President Uhuru Kenyatta for crimes against humanity, including murder, deportation, rape, persecution and inhumane acts allegedly committed by his supporters in the violent aftermath of Kenya’s 2007 elections.

“The timing was not right for me as the president of the United States to be visiting Kenya when those issues are still being worked on, and hopefully at some point resolved,” Obama said.

He noted he’s visited Kenya several times previously and expects he will in the future.

Obama and Zuma appeared at the Union Buildings that house government offices and the site of Mandela’s 1994 inauguration as the country’s first black president after 27 years behind bars for his activism.

The 94-year-old Mandela has been in a nearby hospital for three weeks after being admitted with a lung infection. Zuma told reporters that Mandela is in critical but stable condition and the whole nation is praying that he will improve.

Obama and his wife visited with some of Mandela’s relatives Saturday at the Nelson Mandela Centre of Memory, part of the former president’s foundation. Obama also spoke by telephone with Graca Machel, Mandela’s wife, while she stayed at his bedside in the hospital. Obama said he told the family he hopes Mandela draws comfort from the time he’s spending with loved ones.

“I also reaffirmed the profound impact that his legacy has had in building a free South Africa, and in inspiring people around the world — including me,” Obama said after the visit. “That’s a legacy that we must all honor in our own lives.

The White House said that in accordance with the family’s wishes, Obama was not planning to visit with Mandela.

In nearby Pretoria earlier, Obama revered Mandela as “one of the greatest people in history,” referred to him by his clan name as he praised South Africa’s historic integration from white racist rule as a shining beacon for the world.

“The struggle here against apartheid for freedom, Madiba’s moral courage, this country’s historic transition to a free and democratic nation has been a personal inspiration to me, it has been an inspiration to the world,” Obama said.

“The outpouring of love that we’ve seen in recent days shows that the triumph of Nelson Mandela and this nation speaks to something very deep in the human spirit, the yearning for justice and dignity that transcends boundaries of race and class and faith and country,” Obama said. “That’s what Nelson Mandela represents, that’s what South African at its best represents to the world, and that’s what brings me back here.”

Zuma told Obama he and Mandela are “bound by history as the first black presidents of your respective countries.”

“Thus, you both carry the dreams of millions of people in Africa and in the diaspora who were previously oppressed,” Zuma said, reading from a prepared statement.

On other topics, Obama declined to commit to supporting South Africa’s bid for a permanent seat on the United Nations Security Council. He said the U.N.’s structure needs to be updated and it would be “odd” for an expanded Security Council not to have African representation.

“How we do that and what fashion is complicated, it’s difficult and it involves all kinds of politics,” Obama said.

“Everybody wants a seat at the table, but when it comes time to step up and show responsibility, sometimes people want to be free riders,” Obama said, adding he wasn’t referring to South Africa specifically.

Zuma responded that he wishes the process of change at the U.N. would speed up.

Obama also said he wants to boost trade with Africa and plans to renegotiate an African trade pact to improve it for American businesses. He said he welcomes competition from other nations who have been aggressive in pursuing commercial opportunities in Africa, including China.

“I don’t feel threatened by it. I think it’s a good thing,” he said. He added: “Our only advice is make sure it’s a good deal for Africa.” He said that includes making sure foreign investment employs Africans and doesn’t tolerate corruption or take its natural resources without compensation for Africans.

Obama also is paying tribute to South Africa’s fight against apartheid by visiting the Soweto area Saturday afternoon for a town hall with students at the University of Johannesburg. At least 176 young people were killed in Soweto township 27 years ago this month during a youth protest against the apartheid regime’s ban against teaching local Bantu languages. The Soweto Uprising catalyzed international support against apartheid, and June is now recognized as Youth Month in South Africa.

The university plans to bestow an honorary law degree on the U.S. president.

Protesters under police watch demonstrated outside the university against Obama’s record on surveillance and foreign policy. Demonstrators from a range of trade unions and civil society groups chanted, “Away with intelligence, away,” holding posters depicting Obama with an Adolf Hitler moustache.

“People died in Libya. People are still dying in Syria,” said 54-year-old Ramasimong Tsokolibane. “In Egypt, in Afghanistan in Pakistan drones are still killing people. So that’s why we are calling him a Hitler. He’s a killer.”

Obama has been trying to inspire the continent’s youth to become civically active and part of a new democratically minded generation. Obama hosted young leaders from more than 40 African countries at the White House in 2010 and challenged them to bring change to their countries by standing up for freedom, openness and peaceful disagreement.

Obama wraps up his South Africa stay Sunday, when he plans to give a sweeping speech on U.S.-Africa policy at the University of Cape Town and take his family to Robben Island to tour the prison where Mandela spent 18 of his 27 years behind bars.

Obama has visited the island before, but said it’s a particular privilege to bring his daughters back to learn its lessons.