Kenya's Supreme Court has annulled the result of last month's presidential election, citing irregularities, and ordered a new one within 60 days.
The election commission had declared incumbent Uhuru Kenyatta the winner by a margin of 1.4 million votes.
Raila Odinga, Mr Kenyatta's opponent, said the commission was "rotten" and demanded resignations and prosecutions.
President Kenyatta said he would respect the court's decision but also branded the judges "crooks".
Other elections in Africa have been annulled or cancelled but this appears to be the first time on the continent that an opposition court challenge against a presidential poll result has been successful.
Chief Justice David Maraga said the 8 August election had not been "conducted in accordance with the constitution" and declared it "invalid, null and void".
He said the verdict was backed by four of the six Supreme Court judges.
The announcement drew cheers from opposition supporters both inside and outside the courtroom.
The court ruling did not attribute any blame to President Kenyatta's party or campaign.
What did the judges say was wrong?
Justice Maraga said the election commission had failed "to conduct the presidential election in a manner consistent with the dictates of the constitution".
He said the commission had committed irregularities "in the transmission of results", adding that the court would provide details in a full judgment within 21 days.
Dissenting judges said that the Nasa opposition alliance - which had petitioned the Supreme Court - failed to prove claims that the polls had been rigged.
The election sparked days of sporadic protests, in which at least 28 people were killed. The vote had raised fears of major political violence - as was the case after a disputed poll in 2007.
How have the two political sides reacted?
Mr Odinga, 72, said the ruling marked "a historic day for the people of Kenya and by extension for the people of the continent of Africa".
He said: "It is now clear that the entire [electoral commission] is rotten.
"It is clear that the real election results were never shared with Kenyans. Someone must take responsibility."
Mr Odinga added: "We won the elections and we are going to win them again."
President Kenyatta, in a televised address, said that it was "important to respect the rule of law even if you disagree with the Supreme Court ruling".
He called for calm, saying: "Your neighbour will still be your neighbour, regardless of what has happened... My primary message today to every single Kenyan is peace. Let us be people of peace."
Mr Kenyatta, 55, added: "We are ready to go back again to the people with the same agenda that we delivered to the people."
The president was more combative later at a rally of supporters in a market in Nairobi.
He referred to Justice Maraga and his fellow judges as wakora (crooks in Swahili), saying they had "decided to cancel the election". He warned the chief justice that as the poll had been annulled he was now the president again, not president-elect.
"Do you understand me? Maraga should know that he is now dealing with the serving president," Mr Kenyatta said.
"We are keeping a close eye on them. But let us deal with the election first. We are not afraid."
And the electoral commission?
Chairman Wafula Chebukati noted the ruling and said there would be "changes to personnel" ahead of the new election.
He invited the director of public prosecutions "to prosecute any of our staff that may have been involved in violations".
But he ruled out resigning, saying he had not been accused of wrongdoing.
What have the international monitors said?
After the election, international monitors from the EU, the African Union and the US had said there was no major fraud on polling day and urged Mr Odinga to concede.
On Friday, Marietje Schaake, the head of the EU Observer Mission, said the court ruling represented "a historic day for Kenya and we have always said that people who feel aggrieved should seek the path of the courts".
She said the monitors had at the time pointed to irregularities and encouraged the Kenyan authorities to deal with them.
Ms Schaake said the monitors were awaiting the full details of the ruling.
A huge victory for Odinga
Dickens Olewe, BBC News
Raila Odinga will feel vindicated against accusations that he was just being a bad loser in challenging President Kenyatta's win.
However, this historic decision is a massive indictment of the electoral commission. It is therefore no surprise that the opposition Nasa coalition is now calling for a new team to manage the next elections.
This is also a setback for the international, and some local, election observers, who profusely praised the election as free, fair and credible.
People will be watching for the reaction of former US Secretary of State John Kerry, who was the head of the mission for US NGO, the Carter Centre, whose positive assessment of the election was used in court.
Regardless of the winners and losers following the ruling, this is a proud moment for Kenya. The litigation and debate on the merits of the election was done at the Supreme Court and not on the streets.
Chief Justice Maraga said it best in his opening statement: "The greatness of a nation lies in its fidelity to the constitution and the strict adherence to the rule of law."