He said it was the first example in Africa in which a court nullified the re-election of an incumbent.
The election on Aug. 8 was conducted peacefully and was largely praised by international observers. But David Maraga, the court’s chief justice, declared the result “invalid, null and void” after siding with the opposition, which had argued that the vote had been electronically manipulated to assure a victory for President Uhuru Kenyatta.
Mr. Kenyatta, 55, had been re-elected with 54 percent of the vote, easily surpassing the 50 percent threshold needed to avoid a runoff. His main challenger, Raila Odinga, 72, who petitioned the Supreme Court to nullify the election, had received about 44 percent, a difference of about 1.4 million votes. A parallel tally by domestic observers endorsed the official result.
The decision came as a surprise, even to Mr. Odinga and his supporters, who had complained about election irregularities. A top election official in charge of voting technology was killed about a week before the election, and although the casting of ballots went smoothly, the electronic transmission of vote tallies was flawed, leading the opposition to assert that as many as seven million votes had been stolen.
The Independent Electoral and Boundaries Commission, which was in charge of the vote, “failed, neglected, or refused to conduct the presidential election in a manner consistent with the dictates of the Constitution,” the court said.
The six-judge Supreme Court found no misconduct on the part of the president, Mr. Kenyatta, but it found that the commission “committed irregularities and illegalities in the transmission of results” and unspecified other issues.
“Irregularities affected the integrity of the poll,” Justice Maraga told a stunned courtroom.
A new vote means that candidates will have to start campaigning again and possibly raise millions of dollars: Elections in Kenya generally cost about $1 billion, including spending by the candidates during the campaign and by the government to hold the election.
Thousands of people in the opposition strongholds of Kisumu, Mombasa and parts of Nairobi streamed into the streets and whooped with joy after the news was announced on Friday. Supporters of Mr. Kenyatta in Gatundu, his hometown, were subdued.
“I am happy to be Kenyan today,” said Mr. Odinga, a former prime minister now in his fourth run for the presidency. “It is a historic day for the people of Kenya, and by extension the people of Africa.”
“This is a precedent-setting ruling,” he said, adding that it was the first time in the history of African democratization that “a ruling has been made by a court nullifying irregular presidential elections.”
Mr. Odinga, said that his team planned to take members of the electoral commission to court, saying that they had “committed a criminal act” and belonged in jail.
Mr. Kenyatta, the president, said he respected the ruling and called on all Kenyans to respond peacefully, but he also made clear his anger toward the court.
“Millions of Kenyans queued, made their choice, and six people have decided that they will go against the will of the people,” he said.
Security had been increased on Friday in opposition strongholds amid concern that a ruling in favor of either side could provoke protests or worse. Kenya experienced postelection violence after presidential votes in 2007, 2013 and last month, when at least 24 people were killed, most of them by security forces.
“My concern is that no matter what the court says, the losers will react violently,” John Campbell, a senior fellow for Africa policy at the Council on Foreign Relations and a former ambassador to Nigeria, said before the ruling.
Mr. Campbell expressed concern that “neither Kenyatta nor Odinga prepared their followers for the possibility of losing.”
Immediately after the court’s announcement, however, the atmosphere was more of joy than fear, and there were no immediate reports of violence in strongholds on the losing side, including Gatundu. There, supporters were seen carrying mock coffins with the words “R.I.P. Jubilee” painted on the sides, referring to Mr. Kenyatta’s Jubilee Party.
The Supreme Court, which has bolstered its independence in recent years but had still been viewed by many Kenyans as under government influence, was facing pressure to set out arguments that would persuade people on either side, said Mr. Omondi, the country director for the National Democratic Institute.
The case was an opportunity for the judiciary to truly show its independence, he added, especially after it came under intense criticism for mishandling a similar petition by Mr. Odinga in 2013, which the presiding judge alluded to on Friday at the start of his remarks.
The election controversy hinged on two paper forms that legally validate the ballots — one from each of the country’s 40,883 polling stations and the other from 290 constituencies. Representatives from rival parties were required to approve the forms before they were scanned and electronically transmitted to a national tallying center in Nairobi, where they were to be put online immediately so they could be crosschecked.
But the electronic system, which had been overseen by Christopher Chege Msando, the election official who was killed, broke down. Therefore, only the results, not the forms, were sent to the national tallying center, often by text message.
International election observers were quick to praise the electoral body after the vote, saying there was no evidence that the votes had been tampered with at polling stations and that the paper forms would show clearly who had won. The observers assumed the forms would be easily verifiable and would be matched with figures texted to the tallying center by party officials.
But when Mr. Kenyatta was initially declared the winner, just hours after voting ended, almost none of the forms from the polling stations were online, even though the electoral commission had had a week to receive scanned images of the results.
A couple of days later, the commission announced that about 10,000 forms were unaccounted for, sowing even more doubt and suspicion over its credibility.
“The scenario was similar to that of the Bermuda Triangle, where no one knows how ships disappear,” said Pheroze Nowrojee, a lawyer representing Mr. Odinga and the National Super Alliance, the opposition umbrella group.
The electoral commission said it had presented the forms, a claim that was verified in a report by the registrar of the Supreme Court. However, that report found that a third of the forms had lacked security features like watermarks or serial numbers, which election observers saw as evidence that the forms were probably false.
Defending the integrity of the election, Wafula Chebukati, the chairman of the election commission, noted that the focus of the decision was on the transmission of the results, not on the voting or the counting of the ballots. He urged investigators to prosecute “any of our staff that may have been in violation of the Elections Offenses Act.”
Walter Mebane, a professor of statistics and political science at the University of Michigan who studies elections worldwide, volunteered to run the voting results through a computer model he developed to detect electoral fraud. Based on statistics only, and without knowledge of the intricacies of Kenyan politics, he and his team found patterns that showed widespread manipulation.
“It was unlike any data set I had ever seen,” he said. “Every single indicator came up signaling anomalies. It’s a huge red flag that something weird is going on.”