Nelson Mandela's long walk from apartheid prisoner to South African president remade a country and inspired the world.

Mandela died peacefully at home in Johannesburg aged 95 after spending months in critical condition following treatment for a lung infection.

Twenty-three years earlier, on February 11, 1990, Nelson Rolihlahla Mandela emerged, greying but unbowed, from 27 years detention for opposing the white-minority apartheid regime.

It was a defining moment of the 20th century.

In freeing the world's most famous political prisoner, President FW de Klerk sent an unequivocal message: after centuries of subjugation, millions of other black South Africans would soon be free too.

Apartheid was over.

"I greet you all in the name of peace, democracy and freedom for all," a 71-year-old Mandela said in his first public speech in 27 years.

"I stand here before you not as a prophet, but as a humble servant of you, the people."

Devoid of self-pity, he reached out to the same people who jailed him and who brutalised fellow blacks to preach "true reconciliation" in what was, and remains, a deeply scared country.

"He came out a far greater person than the man who went in," said former archbishop Desmond Tutu.

"He had learned to understand the foibles and weaknesses of human beings and to be more generous in his judgement of others."

Four years after his release -- and just a year after he received the Nobel Peace Prize -- South Africans would vote in droves to elect Mandela the country's first black president.

As that rarest of politicians, a leader imbued with moral force, Mandela was never likely to lose.

A once-in-a-lifetime leader

But his task in office was immense, nothing less than preventing a civil war.

"We enter into a covenant that we shall build a society in which all South Africans, both black and white, will be able to walk tall, without any fear in their hearts, assured of their inalienable right to human dignity -- a Rainbow Nation at peace with itself and the world," he declared on being sworn in.

He succeeded in preventing serious racial violence in part through his easy manner and mastery of symbolism.

Perhaps two of his finest moments as a reconciler came when he had tea with the widow of apartheid architect Hendrik Verwoerd and when he donned the Springbok rugby jersey to congratulate the mainly white team's victory in the 1995 Rugby World Cup.

Mandela remains a unifying symbol in a country still riven by racial tensions and deep inequality.

"His life tells a story that stands in direct opposition to the cynicism and hopelessness that so often afflict our word," US President Barack Obama wrote in the foreword to Mandela's most recent autobiography.

But crime, grinding poverty and corruption scandals have effectively ended the honeymoon enjoyed after Mandela ushered in the "Rainbow Nation".

"Mandela, in a sense, was a once-in-a-hundred-year phenomenon," said Frans Cronje of the Institute for Race Relations.

"Thinking that South Africa would maintain that level or that standard of governance, of attitude, of role in international politics, I think was expecting too much."

Born in the village of Mvezo in one of South Africa's poorest regions, the Transkei, on July 18, 1918, Rolihlahla Dalibhunga Mandela was the great-grandson of a Tembu king.

He was given his English name "Nelson" by a teacher at his school.

An activist since his student days at the University of Fort Hare, Mandela opened the first black law firm in Johannesburg in 1952, along with fellow activist Oliver Tambo.

He became commander-in-chief of Umkhonto we Sizwe (Spear of the Nation), the armed underground wing of the African National Congress, in 1961, and the following year underwent military training in Algeria and Ethiopia.

"An ideal for which I am prepared to die"

After more than a year underground, he was arrested and in 1964 sentenced to life in prison during the Rivonia trial where he delivered a speech that was to become the manifesto of the anti-apartheid movement.

"During my lifetime, I have dedicated myself to this struggle of the African people. I have fought against white domination and I have fought against black domination. I have cherished the ideal of a democratic and free society. ... It is an ideal for which I am prepared to die."

Mandela was jailed on Robben Island for 18 years before being transferred in 1982 to Pollsmoor prison in Cape Town and later to Victor Verster prison in nearby Paarl.

Throughout his incarceration international pressure increased on South Africa.

Then, in 1989 hardline President P.W. Botha was replaced by the more conciliatory F.W. De Klerk.

A year later, De Klerk ordered Mandela's release.

"I wish to put it plainly that the government has taken a firm decision to release Mr Mandela unconditionally," he told a shocked parliament.

"The time for negotiation has arrived." he said, adding: "The alternative is growing violence, tension and conflict."

Mandela's presidency, like that of US president Abraham Lincoln or British prime minister Winston Churchill, will not be remembered for legislative achievements.

He served only one five-year term, and after his retirement in 1999 he devoted his considerable energy -- despite increasing physical frailty -- to mediating conflicts, especially the war in Burundi.

In 1998, on his 80th birthday, Mandela, after having divorced his second wife Winnie Madikizela-Mandela, married Graca Machel, the widow of Mozambican president Samora Machel.

Having been deprived of seeing his own children grow up while he was incarcerated, Mandela dedicated much time to improving the lives of youngsters, drumming up money from businesses to build schools in remote areas.

At age 83, he was diagnosed with prostate cancer and successfully underwent treatment. Throughout his life he suffered from respiratory ailments.

He was diagnosed with early-stage tuberculosis while in prison in 1988.

In May 2004, Mandela announced that he was scaling back his public schedule to enjoy "a much quieter life" with his family and friends.

Eight months later, Mandela convened the press at his home to announce that his only surviving son had died of AIDS in a bid to encourage more openness about the disease.

In January 2011 he suffered a lung infection, which recurred in late 2012 and again in late March.

Mandela is survived by his wife Graca and daughters Maki, Zindzi and Zenani and dozens of grandchildren and great-grandchildren.

One of Mandela's last forays on the world stage was to help bring the World Cup to South Africa in 2010, the first time the tournament was held in Africa. He delighted the crowds at the final with a surprise appearance on the back of a golf buggy.

After the World Cup, President Jacob Zuma said the surge of national pride around the tournament had brought the country close to realising Mandela's vision.

"We came very close if we did not fully achieve your dream, Tata (grandfather), of one nation united in its diversity, celebrating its achievements and working together."