Media playback is unsupported on your device Media caption Mahinda Rajapaksa's office said he would ensure a smooth transition

Sri Lanka's long-time leader Mahinda Rajapaksa has been defeated in the presidential election.

Official results showed Maithripala Sirisena, a former ally of the incumbent, had won 51.3% of the vote.

Mr Rajapaksa, in office since 2005, said on Twitter he looked forward to a peaceful transition of power.

His supporters credit him with ending the civil war and boosting the economy, but critics say he had become increasingly authoritarian and corrupt.

Mr Sirisena had already received promises of support from Tamil and Muslim leaders before the election.

But the result shows he also picked up a significant portion of the majority Sinhalese vote, most of whom solidly supported Mr Rajapaksa in previous elections.

Mr Sirisena was sworn in hours after the result was announced, and promised future election campaigns would be "much more mature".

"Even though [state media] carried out character assassination and vilified me, I can say I had the maturity to bear it all as a result of my long political experience," he told crowds of supporters in Colombo.

Image copyright AFP Image caption Maithripala Sirisena was a surprise opponent when he defected to run against his former friend

Image copyright AFP Image caption His supporters were quick to celebrate the unlikely victory

Charles Haviland, BBC News

The two biggest minorities - Tamils and Muslims - voted for Mr Sirisena in large numbers, and probably swung the vote his way. Their vote was more anti-Rajapaksa than pro-Sirisena.

Mr Sirisena was picked by the unwieldy opposition coalition for his Sinhalese appeal, and he has done little to reach out to the minorities on the campaign trail. It will be difficult for him to ignore their grievances now, including constitutional changes for a settlement of the ethnic grievances that fuelled the long war.

Questions also surround Bodu Bala Sena - the hard-line Buddhist monks' organisation that has peddled hatred and violence against Muslims and some Christians for the past two years. Will it simply disappear with the demise of a government that - at the very least - tolerated it?

Life after Rajapaksa

The incumbent was seeking a third term in office after he changed the constitution to scrap the two-term limit.

But before the results were announced, Mr Rajakpaksa's press officer said the president "concedes defeat and will ensure a smooth transition of power bowing to the wishes of the people".

Both Mr Rajapaksa and Mr Sirisena are Sinhalese, the majority ethnic group in Sri Lanka.

They were allies until November when Mr Sirisena, the health minister in Mr Rajapaksa's government, announced his surprise candidacy.

Many in Sri Lanka are stunned by these events - not just because they have a new president, but because democracy has worked and there has been so little of the vote rigging, intimidation and election violence of the past, reports the BBC's Jill McGivering.

High Tamil turnout

Image copyright Getty Images Image caption There was a strong turnout in the elections and no reports of major disruption to voting

Turnout in many areas was above 70%, roughly in line with previous elections, with no reports of major incidents disrupting the voting process.

In Jaffna and Trincomalee, two of the main Tamil strongholds, turnout was higher than previous national elections.

The build-up to Sri Lankan elections is usually blighted by dozens of deaths, but this year just one election-related death was reported.

Mr Rajapaksa was last elected in 2010 when he defeated his former army chief Sarath Fonseka, who was later jailed on charges of implicating the government in war crimes.

But his critics say he became increasingly authoritarian and failed to tackle the legacy of Sri Lanka's civil war, which left the Tamil areas in the north impoverished and embittered.

Both sides in the war were accused of atrocities, but an inquiry set up by the government that largely exonerated the army was dismissed by rights groups as flawed.