CAPE TOWN — In the foreword to Nelson Mandela’s 2010 book of letters, President Obama wrote that “even when little sunlight shined into that Robben Island cell, he could see a better future — one worthy of sacrifice.”

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.