He has "been discharged from hospital following a sustained and gradual improvement in his general condition. The former president will now receive home-based high care," presidency spokesman Mac Maharaj said in a statement
This latest stay in hospital was 94-year-old Mandela's third since December.
Mandela, who was South Africa's first black president, was admitted to the hospital on March 27. He has received treatment for a recurring lung infection and pneumonia.
In December, he was admitted for 18 days for treatment of the lung infection and surgery to extract gallstones. It was his longest stint in hospital since his release from prison in 1990.
Mandela had a long history of lung problems, dating back to the time when he was a political prisoner on Robben Island during apartheid. While in jail he contracted tuberculosis.
Mandela, who served as president from 1994 to 1999 and is widely regarded as the father of the nation for leading the struggle against apartheid and for democracy.
Meanwhile, Maharaj said that President Jacob Zuma extended his gratitude to all South Africans and people around the world for their support. "President Zuma thanks the hard working medical team and hospital staff for looking after Madiba so efficiently," he said, using Mandela's clan name.
Mandela is fine, getting better in hospital: Wife
Mandela's health improves, breathes 'without difficulty'
Mandela 'much better', making 'steady progress'
Mandela still in hospital, making 'steady progress'
Nelson Mandela's condition improves