The court upheld a petition by opposition candidate Raila Odinga, who said the re-election of President Uhuru Kenyatta on August 8 was fraudulent.
"The presidential election was not conducted in accordance with the constitution, rendering the declared results invalid, null and void," Chief Justice David Maraga said, ordering fresh elections within 60 days.
In a decision that surprised many observers, including Odinga and his supporters, four out of six justices agreed with opposition arguments that the electoral commission committed irregularities invalidating the vote. It also raised questions for international monitors, who had declared the election fair.
Odinga appeared jubilant as he welcomed what he called a "precedent-setting ruling."
"For the first time in the history of African democratization, a ruling has been made by a court nullifying the election of a president," he said. "This indeed is a very historic day for the people of Kenya and by extension the people of the continent of Africa."
In a televised address to the nation Friday, Kenyatta said he would respect the ruling.
"I disagree with it because as I've said, millions of Kenyans queued, made their choice, and six people have decided that they will go against the will of the people," he said.
Kenyatta said his primary message was for all Kenyans to keep the peace. "Your neighbor will still be your neighbor regardless of whatever has happened," he said. "Regardless of their political affiliation, regardless of their religion, regardless of their color, regardless of their tribe."
But on Saturday, Kenyatta criticized the ruling, the chief justice and the judicial system.
"Every time we do something, a judge comes out and places an injunction. It can't go on like this," Kenyatta said before members of his party in a 30-minute televised speech from his official residence.
Kenya has "a problem" with its judiciary that must be fixed, he said.
"Maraga thinks he can overturn the will of the people," Kenyatta said. "We shall show you in 60 days that the will of the people cannot be overturned by one or two individuals."
As news of the court's decision spread, cheers and celebrations could be heard in parts of the capital, Nairobi. In its Kibera slum, an opposition stronghold where some post-election violence erupted last month, hundreds of supporters danced and sang in the streets, some chanting "Uhuru must go!"
"It does mean a lot to me. I am sure Kenya will be at a better place. I am really happy about the decision," Roseyln Aoko told CNN.
"I am really happy about today. I have not even had lunch since I heard the results," said Margaret Akinyi, 36.
But it's not yet clear if the ruling will spark public protests.
Although Kenya's 2013 election was mainly peaceful, the country plunged into widespread violence in the aftermath of the 2007 vote. More than 1,000 people were killed in months of bloodshed after Odinga -- defeated by then-President Mwai Kibaki -- claimed the vote was rigged.
After Kenyatta was declared the winner
last month by 54% to 45% over Odinga, sporadic violence erupted in some areas
, claiming the lives of at least 24 people.
Odinga is a longtime challenger who has yet to claim the presidency. Kenyatta, the 55-year-old son of Kenya's founding father, has already served one five-year term.
Kenyatta lawyer: 'Very political decision'
Kenyatta's lead counsel, Ahmednasir Abdullahi, said in court that his client wanted to see the full judgment to understand how the alleged irregularities would "obliterate" his winning margin of 1.4 million votes.
"My lord it's obvious, and I'm not afraid to say, that this is a very political decision you have made this morning, but we will live with the consequences," Abdullahi said. He added that the will of the people would prevail.
The court has not yet published its full written ruling explaining why the election was invalid but has 60 days to do so.
One of the most contested aspects of the election was the apparent discrepancy between the electronic results as transmitted and the manual count.
The head of Kenya's electoral body, the Independent Electoral and Boundaries Commission, suggested that discrepancy was the basis for the court's ruling.
"The focus of the decision is in the transmission of presidential results. Therefore there were no aspersions cast on the voting and the counting of the votes," Chairman Wafula Chebukati said.
Chebukati said the commission was committed to ensuring that the new election "is done in accordance with the constitution, the relevant laws" and urged the prosecution of any staffer found to have broken the law. He also said he had no plans to step down.
"The commission urges all Kenyans to remain calm and restrain themselves from any political rhetoric that may undermine the stability and cohesion of our country," he said.
In his address after the ruling, Odinga said he had "no faith" in the electoral commission. "They have committed criminal acts. Most of them actually belong in jail and therefore we are going to ask prosecution for all the electoral commission officials who have committed this monstrous crime against the people of Kenya."
Speaking later at a rally, he also condemned international election monitors.
"With this courageous verdict we put on trial the international observers who moved so fast to sanitize fraud. Their role must be examined as it is highly politicized and currently puts status quo and stability ahead of a credible election," he said.
Amnesty International's country director for Kenya, Justus Nyang'aya, said the "ruling demonstrates the independence of Kenya's judiciary and sets an example for the rest of the world." He urged all parties to comply with the judgment and called on police to exercise restraint in their handling of celebrations or protests.
Most of the demonstrators in last month's post-election were supporters of Odinga, who had called the vote rigged. National election officials dismissed the accusations, however, saying it was free and credible.
More than 400 international election monitors were deployed across the country to monitor voting, the tallying process and the post-election period.
Former US Secretary of State John Kerry, an election observer for the Carter Center, said then that the election was not rigged despite "little aberrations here and there."
In one unexplained incident, the head of information technology for Kenya's Integrated Electoral Management System, Chris Msando, was found slain days before the vote
. His department is responsible for voter-identification and result-transmission technology.
Any unrest in Kenya could have ripple effects far beyond the nation of 47 million people.
As the largest economy in East Africa, Kenya is a crucial trade route to the continent and provides an important buffer of stability in a region that includes the fledgling Somali government and the politically tense Sudan and South Sudan.
Trading on Kenya's stock exchange, the Nairobi Securities Exchange, was briefly suspended following the ruling
but has since resumed.