Nelson Mandela has been discharged from hospital following an improvement in his condition, according to the South African government.
TV footage showed an ambulance carrying Mandela arriving at his home in Houghton, Johannesburg.
Mandela, seen universally as the father of the nation, will now receive "home-based high care", the statement from President Jacob Zuma's office said.
South Africa's first post-apartheid leader, had been receiving treatment for pneumonia after being admitted to the hospital in the South African capital of Pretoria on the night of 27 March.
It was his third trip to hospital since December, due to a recurring infection that required a procedure to drain fluid from his lung.
In his statement, Zuma thanked the medical team and hospital staff that cared for Mandela. Zuma said there had been "a sustained and gradual improvement in his general condition".
Mandela served as South Africa's first black president from 1994 to 1999, and was awarded the Nobel Peace Prize in 1993.
"Former President Nelson Mandela has been discharged from hospital today, 6 April, following a sustained and gradual improvement in his general condition," Zuma said in his statement.
"The former president will now receive home-based high care. President Zuma thanks the hard working medical team and hospital staff for looking after Madiba so efficiently."
Madiba is Mandela's clan name.
The statement added that Zuma "extended his gratitude to all South Africans and friends of the Republic in Africa and around the world for support."
Since stepping down as president in 1999, Mandela acted as a high-profile ambassador for the country until he retired from public life in 2004.
In December, Mandela 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.
Mandela contracted tuberculosis in the 1980s during his imprisonment on Robben Island, where he served 18 years of a 27-year sentence for sabotage. His lungs are believed to have been damaged while working in a prison quarry.