Kenya election.
Deputy Prime Minister Uhuru Kenyatta casts his ballot in the town of Gatundu this week.

Kenya's knife-edge election has been won by Uhuru Kenyatta, the country's deputy prime minister, who faces trial at the International Criminal Court in The Hague for crimes against humanity after violence engulfed the last polls in 2007.

Kenya's electoral commission confirmed the results on Saturday 9 March, with Kenyatta winning 50.07 percent, narrowly avoiding a run-off ballot.

Kenyatta's main rival, prime minister Raila Odinga, polled 43.3 percent on a record turnout of 86 percent.

Odinga contested the results, alleging massive vote-rigging and saying he would challenge the "tainted election" in the Supreme Court.

He said the electoral commission had "failed Kenyans" and that democracy itself was "on trial".

But he also appealed for calm, saying: "Any violence could destroy this nation forever."

Odinga's chief campaign manager Eliud Owalo said the "process was fraudulent". His Cord alliance had complained during the campaign that votes from 11 constituencies were missing, in effect leaving him more than 250,000 votes short.

But the Independent Electoral and Boundaries Commission said that while the elections had been complex and difficult, they had also been credible and transparent.

IEBC chairman Issack Hassan said Kenyans had voted "calmly, patiently, proudly and peacefully in the full glare of whole world".

He praised the candidates who had conceded defeat and urged others to follow suit.

After a race that divided the nation along tribal lines, security services were on high alert for what were the first elections since 2007, when more than 1,000 people were killed in violence surrounding the last presidential vote.

The charismatic Kenyatta, 51, whose father was the country's founding president and whose first name means "freedom" in Swahili, is one of Africa's richest men. He is set to face trial at the International Criminal Court over violence that followed the 2007 polls, and stands accused of fuelling the communal violence of 2007.

If he is declared president-elect, Kenya will become the second African country after Sudan to have a sitting president indicted by the International Criminal Court.

The result will pose a dilemma for Kenya's western donors, with the United States and Britain warning before the vote that diplomatic ties would be complicated by a Kenyatta win. Kenyatta's running mate William Ruto has also been indicted by the International Criminal Court.

How western capitals deal with Kenya under Kenyatta will depend heavily on whether he and Ruto co-operate with the tribunal, according to diplomatic sources.

"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 cooperate to clear their names.

The ICC agreed to postpone Ruto's trial by a month until May after his lawyers complained of not having enough time to prepare his defence.

Joyous supporters of Kenyatta thronged the streets in his tribal strongholds in the early hours of Saturday 9 March, lighting fluorescent flares and waving tree branches.

In Nakuru, a Rift Valley town which voted massively for Kenyatta's coalition, crowds packed the streets dancing frenetically and waving foliage in the air.

A close adviser to Odinga said he would not concede the election and would launch a legal challenge if Kenyatta was officially declared the victor.

"He is not conceding the election. If Uhuru Kenyatta is announced president-elect then he will move to the courts immediately," said the adviser, Salim Lone.

Odinga's camp had said during tallying that the ballot count was deeply flawed and had called for it to be halted.

Provisional figures displayed by the electoral commission showed Kenyatta won 6,173,433 votes out of a total of 12,338,667 ballots cast. Odinga secured 5,340,546 votes.

Kisumu, the biggest city in Odinga's tribal heartland and a flashpoint in the violence five years ago, was calm early on Saturday.

Odinga's camp had said even before the result that they were considering legal action, but promised to pursue it through the courts and the newly reformed judiciary.

That contrasted with 2007, when Odinga said he could not trust the judiciary to treat his case fairly.

Anyang' Nyong'o, secretary general of Odinga's party, said his party would file a petition at the Supreme Court "because the process has been awful and there's evidence to that effect".

Asked whether he thought there had been systematic rigging in favour of one candidate he said: "I think so. There's evidence to that effect."

Kenyatta's camp also complained about delays in counting, though many Kenyans remarked on how transparent the process had appeared, compared to previous votes.

Kenya introduced an electronic voting system to eliminate vote-rigging, but the count was hampered by repeated technical errors.

Turnout reached 86 percent of a total 14.3 million eligible voters, in a nation where tribal loyalties largely trump ideology at the ballot box.