On Sunday, Mr. Obama stood in that same tiny prison cell — now a monument to Mr. Mandela, South Africa’s first black president — and showed his wife and two daughters the place where Mr. Mandela spent 18 of his 27 years behind bars during his long campaign to end his country’s policies of racial apartheid and oppression.

Later, Mr. Obama again invoked the legacy of Mr. Mandela, 94, who remained in critical condition at a Pretoria, South Africa, hospital, during a speech to the African people he delivered from the University of Cape Town.

In the speech, he called Mr. Mandela the ultimate testament to the process of peaceful change and said his daughters better understood his legacy now. “Seeing them stand within the walls that once surrounded Nelson Mandela, I knew this was an experience they would never forget,” Mr. Obama said.

“Each time a man stands up for an ideal, or acts to improve the lot of others, or strikes out against injustice, he sends forth a tiny ripple of hope, and crossing each other from a million different centers of energy and daring, those ripples build a current that can sweep down the mightiest walls of oppression and resistance,” Mr. Kennedy said.

The symbolism of Mr. Obama’s visit was impossible to miss: America’s first black president, whose wife is a descendant of African slaves, said this week that he might not have been elected were it not for Mr. Mandela’s ability to endure imprisonment and emerge to take power without bitterness or recrimination.

In a visitor’s book in a prison courtyard, Mr. Obama wrote that his family was “humbled to stand where men of such courage faced down injustice and refused to yield.”

“The world is grateful for the heroes of Robben Island,” he added, “who remind us that no shackles or cells can match the strength of the human spirit.”

For Mr. Obama, the visit to Robben Island, just off Cape Town, was part of an African trip that has been overshadowed to some extent by concerns about Mr. Mandela’s health. Instead of visiting the former leader, Mr. Obama chose to meet instead with Mr. Mandela’s family on Saturday, joining many in this nation who are passing on their prayers for his recovery.

Mr. Obama had been to Robben Island before, as a senator. In his foreword to Mr. Mandela’s book, “Conversations With Myself,” he recalled trying to “transport myself back to those days when President Mandela was still Prisoner 466/64 — a time when the success of his struggle was by no means a certainty.”

He also toured the limestone quarry where Mr. Mandela and other political prisoners were forced to work. Mr. Obama’s daughters, Malia and Sasha, listened as their tour guide, Ahmed Kathrada, who served prison time with Mr. Mandela, described the area.

Sea birds squawked as Mr. Obama talked to his daughters about the history of the prison island, and of the role it played in the political movement of nonviolence started by Gandhi.

“One thing you guys might not be aware of is that the idea of political nonviolence first took root here in South Africa because Mahatma Gandhi was a lawyer here in South Africa,” the president told them. “When he went back to India the principles ultimately led to Indian independence, and what Gandhi did inspired Martin Luther King.”

Video

Watching and Waiting in South Africa

The Times’s Marcus Mabry reports from Johannesburg, where South Africans are closely watching Nelson Mandela’s condition.

A few minutes later, Mr. Obama and his family gathered in a small courtyard where Mr. Mandela and other prisoners were forced to work, and where they occasionally played sports. Along one wall stood lattices for grapevines behind which Mr. Mandela, while a prisoner, hid pages of a manuscript that eventually became his first book, “Long Walk to Freedom.”

Mr. Kathrada told the president that the pages were smuggled out of the prison. Pointing to a black-and-white photograph of prisoners at work in the courtyard, Mr. Kathrada told the Obamas that guards once took away the prisoners’ hammers and took pictures to show the world that the inmates were only doing light work. The hammers were soon given back, he said.

The Obamas crowded into the tiny cell overlooking the courtyard where Mr. Mandela spent nearly two decades. Inside the sterile, cinder block cell was a toilet, a thin mattress with pillows and a brown blanket. A single window looking into the courtyard has thick, white bars, matching the ones on the door to the cellblock’s hallway.

Mr. Obama lingered in the cell by himself. Photographers captured the moment as he stared past the bars to the bright blue sky and sun shining down on the courtyard. He made no comments as he joined his family.

In his speech in Cape Town a few hours later, Mr. Obama announced plans to embrace the new sense of optimism about Africa’s future by creating American programs to help its nations develop more sustainable food programs, better health care networks and more reliable power grids.

He said the United States would invest $9 billion to help double the access to reliable electricity in sub-Saharan Africa. “The world will be watching what decisions you make. The world will be watching what you do,” Mr. Obama said. “My bet is on the young people who are the heartbeat of Africa’s story. I’m betting on all of you.”

During a separate visit to the Desmond Tutu H.I.V. Center in Cape Town, Mr. Obama hailed its efforts to combat AIDS. Mr. Tutu, the civil rights activist and retired bishop, said Africans prayed for Mr. Obama’s success in promoting peace. “Your success is our success,” Mr. Tutu said. “Your failure, whether you like it or not, is our failure. We are proud of you. You belong to us.”

The president is to leave Cape Town on Monday morning for a day in Tanzania before heading home to Washington on Tuesday.

White House officials have declined to comment about whether Mr. Obama’s travel schedule will change in the event of Mr. Mandela’s death. But it is widely expected that the president would return to South Africa for the funeral.

South Africans are preparing themselves for that moment, with impromptu gatherings in front of Mr. Mandela’s home and at the hospital where he is being treated.

“It matters not how strait the gate/How charged with punishments the scroll,” Mr. Obama read. “I am the master of my fate:/I am the captain of my soul.”

South African news media coverage of Mr. Mandela’s illness on Sunday focused on a bitter feud among Mr. Mandela’s family, which has become divided on where the former president should be buried.

On Friday, 16 members of the Mandela family initiated a lawsuit against Mr. Mandela’s grandson Mandla Mandela, accusing him of secretly exhuming the remains of three of Mr. Mandela’s children in 2011 as part of a ruse to shift the family graveyard closer to his home.

Court documents cited in Sunday’s newspapers contained claims that the ancestral spirits of the Mandela family had been disturbed by the bodies’ removal, which was in turn contributing to Mr. Mandela’s suffering.

In a statement released on Sunday morning, Mandla Mandela said he would contest the lawsuit in court.

Declan Walsh contributed reporting from Johannesburg.

A version of this article appears in print on July 1, 2013, on page A4 of the New York edition with the headline: Obama Visits Prison Cell That Helped Shape Modern South Africa. Order Reprints|Today's Paper|Subscribe