Uhuru Kenyatta, indicted for crimes against humanity, was declared winner of Kenya's presidential election on Saturday with a tiny margin, just enough to avoid a run-off after a race that has divided the nation along tribal lines.
Kenyatta, the son of Kenya's founding president, faces trial after the disputed 2007 presidential vote that unleashed a wave of tribal blood-letting.
With the 51-year-old in the top job, Kenya will become the second African country after Sudan to have a sitting president indicted by the International Criminal Court.
The United States and other Western powers, big donors to the east African nation, said before the vote that a Kenyatta win would complicate diplomatic ties with a nation viewed as a vital ally in the regional battle against militant Islam.
After saying Kenyatta secured 50.07 per cent of the votes, just achieving the more than 50 per cent needed to avoid a second round, the chairman of the Independent Electoral and Boundaries Commission, Issack Hassan, announced:
"I therefore declare Uhuru Kenyatta the duly elected president of the Republic of Kenya," he said. Shortly afterward he handed a certificate of the results to Kenyatta, who had arrived after the declaration. Kenyatta thanked him.
Many in the election centre cheered, although celebrations started in the early hours of Saturday after provisional results showed Kenyatta's victory. Supporters thronged the streets of Nairobi and his tribal strongholds, lighting fluorescent flares and waving tree branches and chanting "Uhuru, Uhuru."
The mood was tense but calm in the heartlands of Kenyatta's rival, Prime Minister Raila Odinga, who also lost in the disputed 2007 vote and trailed this time with 43.3 per cent.
"No Raila, no peace," Odinga supporters chanted as security forces stood by in Kisumu, a city where violence flared in 2007.
Odinga rejected the election outcome on Saturday and said he would challenge it in court.
Odinga's camp had said during tallying that the ballot count was deeply flawed and called for it to be halted. But Odinga asked his supporters not to resort to violence.
Odinga said there was rampant illegality in the electoral process and that "democracy was on trial in Kenya".
"We will therefore shortly move to court to challenge the outcome that the IEBC [Independent Electoral and Boundaries Commission] announced a few hours ago," Odinga told a news conference, adding he would accept the ruling of the court.
"Any violence now could destroy this nation forever, but it would not serve anyone's interests," he said.
Odinga said he would have conceded if the vote was fair.
The election commission, plagued by technical problems that slowed the count, took five days to announce the result.
International observers broadly said the vote and count had been transparent so far and the electoral commission, which replaced an old, discredited body, promised a credible vote.
Kenyatta, the deputy prime minister, achieved the 50 per cent mark by a tiny margin of about 8,400 votes out of the more than 12.3 million that were cast.
Both sides relied heavily on their ethnic groups in a nation where tribal loyalties mostly trump ideology at the ballot box. Kenyatta is a Kikuyu, the biggest of Kenya's many tribes, Odinga is a Luo. Both had running mates from other tribes.
John Githongo, a former senior government official-turned-whistleblower, urged the rival coalitions, Odinga's CORD and Kenyatta's Jubilee, to ensure calm. "Jubilee and CORD, what you and your supporters say now determines continued peace and stability in Kenya. We are watching you!" he said on Twitter.
How Western capitals deal with Kenya under Kenyatta and the extent they would be ready to work with his government will depend heavily on whether Kenyatta and his running mate William Ruto, who is also indicted, cooperate with the tribunal.
"It won't be a headache as long as he cooperates with the ICC," said one Western diplomat. "We respect the decision of the majority of the Kenyan voters."
Both Kenyatta and Ruto deny the charges and have said they will work to clear their names, though Kenyatta had to fend off jibes during the campaign by Odinga that he would have to run government by Skype from The Hague.
"Until now, Kenyatta has been co-operating with the court and we do hope this will continue," said Fadi El-Abdallah, spokesman for the Hague-based court. "This is part of Kenya respecting its legal obligations under international law."
Kenyans hope the vote, which has so far passed off with only pockets of unrest on voting day, would restore their nation's reputation as one of Africa's most stable democracies after killings last time left more than 1,200 dead.
Many Kenyans have said they are determined to avoid a repeat of the post-2007 chaos that brought the economy to a halt.
Church leaders in Kisumu, in the west of Kenya that was devastated five years ago, sought to defuse tension this time.
"Our vote was stolen and we're angry," said Denis Onyango, a 28-year-old mechanic, as hundreds of Odinga supporters gathered with members of the security forces nearby. "Why did they bring such huge security here if the vote was to be free and fair."
Some said it was time to move on. "I urge our candidate to forget the presidency and let the will of God prevail," said cloth vendor Diana Ndonga.
Many shops stayed closed as a precaution in the port city of Mombasa, another Odinga stronghold, but streets were calm.
"We are heading for a bleak future where the economy goes down and international relation sour because of the ICC case," said Athumani Yeya, 45, a teacher in the city.
Others were hopeful that Kenyatta could bring change.
"We are celebrating. Even with the ICC case in Holland, the people of Kenya still have faith in him," said Thomas Gitau, 25, a bare-foot car washer on a main Mombasa street. "We hope he can fix infrastructure and security so we have more jobs."
Odinga's camp had said even before the result that they were considering a court challenge. In 2007, he said the courts could not be trusted to handle the case. Kenyatta's camp had also complained about counting delays and other aspects of the vote.
But many Kenyans said this race was more transparent. Turnout reached 86 per cent of the 14.3 million eligible voters.