Kenya’s Deputy Prime Minister Uhuru Kenyatta has been confirmed as the winner of the presidential election.
Mr Kenyatta won 50.07% of the vote on a turnout of 86%, officials said, narrowly avoiding a run-off ballot.
But main rival Raila Odinga alleged massive vote-rigging and said he would challenge the results of the “tainted election” in the Supreme Court.
Mr Kenyatta is set to face trial at the International Criminal Court over violence that followed the 2007 polls.
He is accused of fuelling the communal violence that saw more than 1,000 people killed and 600,000 forced from their homes.
The Independent Electoral and Boundaries Commission (IEBC) said the latest elections had been complex and difficult – but also credible and transparent.
It said the turnout was the largest ever.
IEBC chairman Issack Hassan, announcing the final vote tallies, said Kenyans had voted “calmly, patiently, proudly and peacefully in the full glare of whole world”.
Mr Hassan praised the candidates who had already conceded victory and urged others to follow suit.
However, Mr Odinga, the current prime minister, said the electoral commission had “failed Kenyans” and that democracy itself was “on trial”.
But after announcing his Supreme Court challenge, he also appealed for calm, saying: “Any violence could destroy this nation forever.”
The BBC’s Gabriel Gatehouse in Nairobi says this was the tightest of races with the narrowest of margins.
He says that how Mr Odinga now handles his supporters will determine whether his dispute stays in the courts or spills out on the streets.
Mr Kenyatta’s Jubilee Coalition party said it was “proud and honoured for the trust” bestowed on it, adding that it had taken a message to the people and that “we are grateful to the people of Kenya for accepting this message”.
Early on Saturday, small groups of Kenyatta supporters celebrated in Nairobi, hooting car horns and singing.
But the newly confirmed president could face difficult relations with Western countries.
In July, he is due to go on trial at the International Criminal Court (ICC) in The Hague for alleged crimes against humanity.
Mr Kenyatta’s running mate, William Ruto, also faces similar charges. Both men deny the accusations.
The ICC has agreed to postpone Mr Ruto’s trial by a month until May after his lawyers complained of not having enough time to prepare his defence.
Countries including the US and UK have hinted that Mr Kenyatta’s election as president would have consequences for their relations with Kenya. The comments have been dismissed in Nairobi as foreign interference.
Kenya’s new electronic voting system was designed to eliminate the chance of vote-rigging, and with it any risk of a repeat of the post-poll violence of 2007.
But the count has been plagued with technical glitches, including a programming error that led to the number of rejected votes being multiplied by a factor of eight.
Mr Odinga’s Cord alliance had earlier complained that votes from 11 constituencies were missing, in effect leaving him more than 250,000 votes short.