* Kenyatta gets outright win with margin of 4,100 votes
* Rival Odinga will not concede election - adviser
* Kenyans waited five days to learn result
* Disputed election five years ago sparked tribal violence (Adds Odinga adviser, voters in Kisumu)
Uhuru Kenyatta, the son of Kenya's founding president, won the presidential election by the slimmest of margins with 50.03 percent, provisional results showed, just enough to avoid a run-off after a race that has divided the nation along tribal lines.
Kenyatta faces trial for crimes against humanity. If he is declared president-elect by the election commission, which has still to announce the official result, Kenya will become the second African country after Sudan to have a sitting president indicted by the International Criminal Court.
In the early hours of Saturday joyous supporters of Kenyatta thronged the streets in his tribal strongholds, lighting fluorescent flares and waving tree branches and chanting "Uhuru, Uhuru," television pictures showed.
Kenyatta's main rival, Prime Minister Raila Odinga, trailed with 43.28 percent of the vote. 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," Salim Lone told Reuters, speaking on behalf of the prime minister.
Odinga's camp had said during tallying that the ballot count was deeply flawed and had called for it to be halted.
To secure an outright win a candidate needed more than 50 percent of the votes. Kenyatta, the deputy prime minister, achieved that but with a margin of just 4,100 of the more than 12.3 million votes cast.
The first-round win, which must be officially confirmed by the Independent Electoral and Boundaries Commission (IEBC), means Kenyans who waited five days for the vote result will not now face a second round that would have prolonged uncertainty.
The winner also needs to get at least 25 percent of the votes in 24 counties out of 47. This is expected to be confirmed by the electoral commission. The IEBC is due to announce the official result on Saturday at 11 a.m. (0800 GMT).
Odinga also lost in a disputed vote in 2007 that led to weeks of tribal killings. His camp has said any challenge will follow the rule of law and Kenyans generally have greater trust in the judiciary now than they did five years ago after reforms.
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.
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.
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.
The result will pose a dilemma for Kenya's big Western donors, with Kenyatta due to go on trial in The Hague accused of orchestrating the tribal violence five years ago.
The United States and other Western states warned before the vote that diplomatic ties would be complicated with a win by Kenyatta, whose running mate William Ruto has also been indicted by the International Criminal Court.
How Western capitals would deal with Kenya under Kenyatta and the extent to which they would be ready to deal with his government will depend heavily on whether Kenyatta and Ruto 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 cooperate to clear their names, though Kenyatta had to fend of jibes during the campaign by Odinga that he would have to run government by Skype from The Hague.
Kenyans hope this vote, which has until now 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 mayhem last time.
"FORGET THE PRESIDENCY"
The city of Kisumu, the biggest in Odinga's tribal heartland and a flashpoint in the violence five years ago, was calm early on Saturday and there appeared little appetite for unrest, even if some believed the poll was flawed.
"I urge our candidate to forget the presidency and let the will of God prevail," said cloth vendor Diana Ndonga.
As businesses in Kisumu began opening, Erick Odhiambo, another Odinga supporter said, "The vote was rigged and I'm not happy. Raila (Odinga) should battle it out in court."
The test will be whether any challenges to the outcome are worked out in the courts, and do not spill into the streets.
Odinga's camp had said even before the result that they were considering legal action, but said they would pursue it through the courts and the newly reformed judiciary.
That is a change from 2007, when Odinga said he could not trust the judiciary at the time to treat the case fairly.
Kenyatta's camp had also complained about delays in counting and other aspects of the process. But many Kenyans said this race was more transparent than previous votes.
Turnout reached 86 percent of the 14.3 million eligible voters, in a nation where tribal loyalties largely trump ideology at the ballot box.