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 some opposition strongholds, with stone-throwing mobs squaring off against Kalashnikov-toting police officers. And Mr. Kenyatta's legal battles are hardly over.
As Kenya's next president, Mr. 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 that he is innocent and that 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 Mr. Kenyatta would lose.
The United States now may have little choice but to work with Mr. Kenyatta because Kenya is one of its closest allies in Africa, serving as a base for everything from running billion-dollar health programs to spying on agents of Al Qaeda. When it comes down to it, several analysts have said, the United States actually needs Kenya more than Kenya needs the United States.
On Saturday afternoon, in front of a hushed courtroom, Kenya's chief justice, Willy Mutunga, read out the verdict upholding Mr. Kenyatta's victory, saying that 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. Mr. Odinga had accused Kenya's election commission of conspiring with Mr. Kenyatta to steal the vote. In the past week, in heated hearings in front of the Supreme Court, Mr. Odinga's lawyers presented evidence of questionable vote tallying.
The Supreme Court itself concluded that 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, Mr. Odinga mopped his face with a handkerchief and said, somewhat mournfully, "The court has now spoken."
He said he would abide by its decision, and he wished Mr. Kenyatta well.
A few hours later, Mr. 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."
Mr. Kenyatta, a son of Kenya's first president and one of the country's richest men, is expected to be sworn in on April 9. His running mate, William Ruto, soon to be deputy president, has also been charged by the International Criminal Court with crimes against humanity, accused of organizing young men to kill villagers during the last election.
The long-awaited verdict caps weeks, if not months, of distraction, anxiety, hope and dread. In 2007 and early 2008, Kenya cracked open in riots and clashes after Mr. Odinga lost the presidential race amid evidence of vote rigging, leaving more than 1,000 dead. The horrific memories from that time have been fresh in the minds of many Kenyans, like little shards of glass painfully embedded just below the surface. Many have feared that another contested election could set off the same type of violence.
On March 4, Kenyans streamed into polling places. The turnout was tremendous, around 86 percent. Some people waited 10 hours on their feet, without any food or drink, to get to the ballot box.
But problems started almost immediately. A new biometric voter identification system failed, and then, after the polls closed, the electronic system to transmit results directly from the polling places to election headquarters crashed. Mr. Odinga's side said it was a conspiracy. The election commission said it was an accident. Election officials then had to tally the results manually, which took days and opened up more possibilities for fraud.
Kenya remained peaceful -- but anxious -- while all this was being sorted out. Mr. Odinga, Mr. Kenyatta and other leaders of all stripes urged their followers not to riot or protest. Television stations played peace messages around the clock.
On March 9, the election commission declared Mr. Kenyatta the winner, saying he had squeaked past the 50 percent threshold to avoid a runoff by less than one-tenth of 1 percent. Mr. Odinga, who won about 43 percent, then filed his lawsuit.
On Saturday night, after the Supreme Court upheld Mr. Kenyatta's victory, protests broke out in several slum areas of Nairobi, the capital, and in Kisumu, Mr. Odinga's ethnic stronghold. His supporters tried to barricade roads with burning tires, but police officers shot in the air and chased them away.
This article originally appeared in The New York Times.