Tensions have also spiked since police said last week that they arrested dozens of Islamic State-linked terror suspects
Heavily armed Indonesian troops were on high alert Tuesday amid fears of civil unrest in the capital Jakarta, as the surprise early announcement of official election results handed Joko Widodo another term as leader of the world's third-biggest democracy.
The election commission had been due to give the divisive poll's final tally on Wednesday, but results were announced early Tuesday instead.
Presidential challenger Prabowo Subianto, a 67-year-old retired general, vowed to take his claims of widespread cheating to court as several thousand supporters rallied in the sprawling capital.
Tensions have also spiked since police said last week that they arrested dozens of Islamic State-linked terror suspects who planned to cause chaos by bombing post-election protests.
On Friday, the US embassy in Jakarta issued a heightened security alert for Indonesia, the world's biggest Muslim majority nation which has long wrestled with Islamist militancy. Other embassies issued similar warnings.
The front of the elections commission office was barricaded with razor wire and protected by scores of security personnel Tuesday, after Subianto warned that mass demonstrations could break out if he lost.
The former military man -- who has strong ties to the Suharto dictatorship which collapsed in 1998 -- has unleashed a stream of rhetoric since unofficial results for the April 17 poll put Widodo ahead by a wide margin.
On Tuesday, he rejected the official results, but called on supporters to remain calm as he pursued "legal avenues" -- candidates have three days to file a formal complaint at Indonesia's Constitutional Court.
Subianto unsuccessfully challenged the 2014 election which he also lost to Widodo.
Analysts and election officials discounted his fraud claims.
"The scale of abuses and errors in the conduct of the election are clearly very minor overall," said Jakarta-based political analyst Kevin O'Rourke.
On Tuesday, thousands of Subianto supporters marched through the streets and held a peaceful protest outside the election supervisory agency's office -- as police in riot gear looked on.
"We have only one request -- that the election is honest and fair," said 35-year-old Dani Firdaus.
Chief Security Minister Wiranto warned that security forces would crack down on mass protests.
"I'm calling on all parties to have a big heart and be a good sport," he said.
"If you lost, please concede."
Widodo, 57, got a rock star welcome when he visited a poor Jakarta neighbourhood where he formally declared victory, as cheering residents hung out of windows to hear him speak.
"Hopefully he can keep making Indonesia better and more modern," resident Septani, who goes by one name, told AFP.
The presidential campaign was punctuated by bitter mudslinging and a slew of fake news online, with much of it aimed at the presidential candidates.
The soft-spoken Widodo -- who pointed to his efforts to boost Southeast Asia's biggest economy with a huge infrastructure push -- stood in stark contrast to fiery nationalist Subianto, a strongman who courted Islamic hardliners and promised a boost to military and defence spending.
A record 245,000 candidates ran for public office in Indonesia's elections, from the presidency and parliamentary seats to local positions -- the first time all were held on the same day.
Widodo and Amin won the country's top jobs with 55.5 percent of the vote against 44.5 percent for Subianto and his wealthy financier partner Sandiaga Uno, the commission said Tuesday.
Some 85.6 million votes were cast in favour of Widodo out of about 154 million, it added.
Widodo scoring thumping victories in holiday hotspot Bali and heavily populated East Java, while Subianto landed big wins in religiously conservative Aceh and West Java.
Nine parties won seats in parliament, led by The Indonesian Democratic Party of Struggle, which Widodo belongs to, and followed by Gerindra and Golkar.