Nelson Mandela has spent his first night at home after being discharged from hospital where he was being treated for pneumonia.
In a statement, South Africa's government said there had been "a sustained and gradual improvement".
The 94-year-old was admitted on 27 March for a recurring infection of the lungs and had fluid drained from them.
Mr Mandela was the country's first black president in the 1990s and is seen by many as father of the nation.
He led the struggle against apartheid (white minority rule) and in 1993 was awarded the Nobel Peace Prize.
The presidency statement read: "Former President Nelson Mandela has been discharged from hospital today, 6 April, following a sustained and gradual improvement in his general condition.
"The former president will now receive home-based high care. President [Jacob] Zuma thanks the hard working medical team and hospital staff for looking after Madiba so efficiently."
Madiba is Mr Mandela's clan name.
The statement continued: "[Mr Zuma] also extended his gratitude to all South Africans and friends of the Republic in Africa and around the world for support."
Mr Mandela's home is in the Houghton district of Johannesburg.
The BBC's Milton Nkosi, outside the residence, says there are many police vehicles parked there, as well as a large number of journalists waiting to hear any update on Mr Mandela's condition.
Local residents expressed relief that Mr Mandela was back home.
S'thembiso Skhosana told Reuters news agency: "We feel very happy for him and for the family, and for the fact that he is out of hospital now and he is reunited with his family."
Mr Mandela stepped down as president in 1999 and acted as a high-profile ambassador for the country until he retired from public life in 2004.
His latest stay in hospital was his fourth in two years.
In December, the ex-leader spent 18 days undergoing treatment for a lung infection and gallstones - his longest period in hospital since leaving prison in 1990.
In February, he was treated for a stomach condition.
Mr Mandela contracted tuberculosis in the 1980s while detained on the windswept Robben Island, where he served 18 years of a 27-year sentence for sabotage.
His lungs are said to have been damaged when he worked in a prison quarry.
Despite his long imprisonment, Mr Mandela forgave his former enemies and as president urged South Africans of all races to work together and seek reconciliation.
His main home is in Qunu, a small rural village in Eastern Cape province, where he says he spent the happiest days of his childhood.
However, doctors have said he should remain at his home in Houghton to be close to Johannesburg's medical facilities.