* Rival Odinga says will challenge vote in court
* International court urges Kenyatta to keep cooperating
* Kenyatta pledges to work with "all nations"
* Police fire teargas at Odinga backers throwing stones
* Disputed election five years ago unleashed tribal violence
Uhuru Kenyatta, indicted for crimes against humanity, was declared winner of Kenya's presidential election on Saturday, but rival Raila Odinga said he would challenge the outcome in court and asked supporters to avoid violence.
Kenyatta, Kenya's richest man and son of its founding president, faces trial on charges of playing a leading role in the wave of tribal killings that followed the disputed 2007 presidential election. His win on Saturday avoided what could have been a divisive a run-off pencilled in for April.
With Kenyatta, 51, in the top job, Kenya will become the second African country after Sudan to have a sitting president who has been indicted by the International Criminal Court.
The United States and other Western powers, big donors to the east African country, said before the vote that a Kenyatta win would complicate diplomatic ties with a nation viewed as a vital ally in a regional battle against militant Islam.
In his acceptance speech, Kenyatta said he and his team would cooperate with international institutions and that he expected the world to respect Kenya's sovereignty.
"We recognise and accept our international obligations and we will continue to cooperate with all nations and international institutions - in line with those obligations."
After saying Kenyatta secured 50.07 percent of the vote, edging over the 50 percent 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."
Shortly afterwards, Hassan handed a certificate of the results to Kenyatta, who had arrived after the declaration. Kenyatta thanked him and went to a nearby university campus in the capital Nairobi where he delivered his acceptance speech.
Many in the election centre cheered, although celebrations started in the early hours of Saturday after provisional results indicated Kenyatta's victory. Supporters thronged the streets of Nairobi and his tribal strongholds, lighting fluorescent flares, waving tree branches and chanting "Uhuru, Uhuru".
Violence flared briefly in Odinga's heartlands where police fired teargas at supporters of the defeated candidate who were throwing stones. "No Raila, no peace!" they chanted at the scene near the western city of Kisumu, which was shattered by violence after the 2007 election.
Last time the bloodshed started immediately after the election results, and analysts predicted that Kenya was likely to escape fighting this time around.
Odinga, 68, said he would have conceded if the vote was fair, adding that there was "rampant illegality" in the electoral process and that "democracy was on trial in Kenya" and he would challenge it in court.
"Any violence now could destroy this nation forever, but it would not serve anyone's interests," he said.
Odinga, who secured 43.3 percent of the vote, had also questioned the election process before the balloting and during the count his party officials had called for tallying to stop.
The election commission, plagued by technical problems that slowed the count, took five days to announce the result. It dismissed accusations of irregularities.
International observers broadly said the vote and count had been transparent so far and the electoral commission, which replaced a discredited body, said it delivered a credible vote.
Kenyatta, now the deputy prime minister, climbed above 50 percent by just 8,400 of the more than 12.3 million votes 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, while 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 his government will depend on whether he and his running mate William Ruto, who is also indicted, work with the tribunal.
Kenyatta says he is innocent of the charges, which allege that he directed a militia accused of murder and rape during the violence after the last election.
Western capitals were swift on Saturday to applaud Kenya's peaceful vote but avoided mentioning Kenyatta, whose election they had said would complicate relations because of the charges of crimes against humanity he faces.
U.S. Secretary of State John Kerry said America had longstanding ties with Kenya, and "will continue to be a strong friend and ally of the Kenyan people". He congratulated the people of the east African country for holding a peaceful vote.
CALL FOR COOPERATION
Both Kenyatta and Ruto deny the charges and have said they will work to clear their names. 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 cooperating 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 passed off with only pockets of unrest on voting day, will 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, which paralysed the economy.
Church leaders in Kisumu sought to defuse tension this time and some Odinga supporters said it was time to move on. "I urge our candidate to forget the presidency and let the will of God prevail," cloth vendor Diana Ndonga said.
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 relations sour because of the ICC case," said Athumani Yeya, 45, a teacher in the city.
Some said Odinga's call for non-violence had calmed nerves.
"Now that we have heard Raila's voice, we will maintain peace and give courts time to do their work. I urge people of this area to remain calm and continue with their normal lives," said Robert Ouko, 24, a transporter.
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 barefoot 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 percent of the 14.3 million eligible voters.