NAIROBI, Kenya — Kenya's Supreme Court on Saturday unanimously upheld the election victory of Uhuru Kenyatta as the country's president, dismissing allegations that the vote had been rigged.

But almost immediately, protests erupted in opposition strongholds, with stone-throwing mobs squaring off against Kalashnikov-toting police officers. And Kenyatta's legal battles are hardly over.

As Kenya's next president, Kenyatta will soon be summoned to the International Criminal Court in The Hague to stand trial on charges of crimes against humanity, accused of using his vast family fortune to bankroll death squads during the chaos that exploded after Kenya's last disputed election in 2007. He says he is innocent and the charges are based on gossip.

But many Western officials believe otherwise, and already the Obama administration has gotten off on the wrong foot, sending the signal that it hoped Kenyatta would lose. The United States now might have little choice but to work with Kenyatta.

On Saturday afternoon, in front of a hushed courtroom, Kenya's chief justice, Willy Mutunga, read the verdict upholding Kenyatta's win, saying the election, held nearly four weeks ago, had been conducted “in compliance with the constitution and the law.”

The second-place finisher, Prime Minister Raila Odinga, seemed to finally swallow his defeat. Odinga had accused Kenya's election commission of conspiring with Kenyatta to steal the vote. In the past week, in heated hearings in front of the Supreme Court, Odinga's lawyers presented evidence of questionable vote tallying.

The Supreme Court itself concluded there had been dozens of errors, though it appears the justices did not feel those errors would have changed the outcome — or they were wary of dragging out what had already become a long and tortuous election period.

On Saturday evening, in a room full of his supporters, Odinga said, somewhat mournfully, “The court has now spoken.” He said he would abide by its decision, and he wished Kenyatta well.

A few hours later, Kenyatta spoke to the nation, saying, “I urge you to accept the election is over.” And he called for Kenyans to come together “above the partisanship.”

On Saturday night, after the Supreme Court upheld Kenyatta's victory, protests broke out in several slum areas of Nairobi, the capital, and in Kisumu, Odinga's ethnic stronghold. His supporters tried to barricade roads with burning tires, but police officers shot in the air and chased them away.