Kenya's Deputy Prime Minister Uhuru Kenyatta has been confirmed as the winner of the presidential election, and vowed to work with his rivals.
He won 50.07% of the vote, officials said, narrowly avoiding a run-off.
But his main challenger, Raila Odinga, alleged massive fraud 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.
After the results were announced, Mr Kenyatta told cheering supporters he would serve all Kenyans "without fear or favour".
Speaking at the Catholic University in Nairobi, he called on Mr Odinga and other leaders to "join us in moving our nation forward."
Earlier, the Independent Electoral and Boundaries Commission (IEBC) said the latest elections had been complex, but also credible and transparent.
It said the turnout, at 86%, was the largest ever
IEBC chairman Issack 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.
In his victory speech, Mr Kenyatta restated his promise to co-operate "with all nations and international institutions".
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.
A new electronic system for transmitting vote results was designed to eliminate the risk of fraud, and thus avoid 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.