US President Barack Obama has toured Robben Island – the jail in which Nelson Mandela was kept for 18 years.
He said he and his family were "deeply humbled" to visit the prison once inhabited by Mr Mandela – who remains critically ill in hospital.
Mr Obama went on to give a speech at the University of Cape Town and launch a multi-billion-dollar electricity initiative.
The US leader did not visit Mr Mandela, but met the Mandela family in private.
Security was stepped up during this final Cape Town leg of his time in South Africa, following clashes on Saturday between riot police and anti-Obama protesters in Soweto.
Mr Obama and the first family visited Mr Mandela’s bleak cell as well as the lime quarry – overlooked by a concrete watchtower – where anti-apartheid fighters including Mr Mandela were forced to undertake hours of back-breaking labour.
Mr Mandela was at the prison for 18 years and his long history of lung problems can be traced to the tuberculosis he contracted there – which he attributed to the dampness of his cell.
Later, Mr Obama wrote in the guest book in the prison courtyard: "On behalf of our family, we’re deeply 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, who remind us that no shackles or cells can match the strength of the human spirit."
Mr Obama also visited a community project before delivering a keynote address at the University of Cape Town.
It was the same venue where 47 years ago, US Senator Robert Kennedy gave his famed "ripple of hope" speech, which gave inspiration to those fighting the racially divisive policies of apartheid rule and linked their struggle with that of the US civil rights movement.
Mr Obama paid tribute to South Africa’s achievements over the past two decades but urged young Africans to fulfil Mr Mandela’s legacy.
"Nelson Mandela showed us that one man’s courage can move the world,” he said.
More needed to be done to tackle poverty and disease, he said, adding that fear too often prevailed in Africa. For as long as war raged, democracy and economic opportunity could not take hold, he said.Mr Obama also announced a $7bn (£4.6bn) five-year initiative to double access to electricity in sub-Saharan Africa, in partnership with African countries and the private sector.
Mr Obama arrived in South Africa from Senegal on Friday. On Monday, he is travelling to Tanzania, the final stage of his African tour.
Meanwhile Mr Mandela’s grandson, Mandla, has said he will oppose a court action brought by the rest of the family, seeking to exhume the bodies of his father, Makgatho, two of Nelson Mandela’s daughters and two other relatives.
The rest of the family want the remains to be reburied in Qunu, where the former South African president wants to be laid to rest, while Mandla, an ANC MP, wants them to stay in the nearby village of Mvezo, Nelson Mandela’s birthplace, where he is building a museum dedicated to his grandfather.
South Africa’s Sunday Times newspaper quotes local chiefs in the area as saying that Madiba, as Nelson Mandela is known in the country, will not be at peace until this issue is resolved.
On Friday, a court granted an interim action saying the bodies could be exhumed and reburied but Mandla Mandela says he was not aware of the case until it was reported in the media and he is now opposing it.