Joko Widodo, who had held off declaring victory after last month's polls, confirmed the win Tuesday.

Thousands of soldiers fanned out across Jakarta on Tuesday after the surprise early announcement of official results in Indonesia's election showed Joko Widodo re-elected leader of the world's third-biggest democracy.

The election commission had been due to give the final tally of the divisive poll on Wednesday, but the results were revealed early Tuesday with little advance notice amid fears of unrest.

Presidential challenger Prabowo Subianto had warned of possible mass uprisings in response to his claims of widespread cheating.

Tensions have also spiked high since police said last week that they arrested dozens of Islamic State-linked terror suspects -- including some who planned to cause chaos by detonating bombs at any post-election protests.

On Friday, the US embassy in Jakarta issued a heightened security alert for Indonesia, the world's biggest Muslim majority nation.

There was a heavy security presence in Jakarta on Tuesday, including in front of the elections commission office, which was barricaded with razor wire and protected by scores of heavily armed troops.

Subianto, a 67-year-old retired general, has targeted the commission by accusing it of allowing a string of election violations.

The former military man -- who has strong ties to the Suharto dictatorship that collapsed in 1998 -- has kept up a steady string of rhetoric since unofficial results for the April 17 poll put bitter rival Widodo ahead by a wide margin.

Widodo, who had held off declaring victory after last month's polls, confirmed the win Tuesday.

"After we're sworn in... we will be president and vice president," Widodo told reporters in Jakarta with running mate Ma'ruf Amin, a Muslim cleric, at his side.

Subianto, meanwhile, rejected the results but called for supporters to remain calm and said his camp would pursue "legal avenues".

Earlier Tuesday, Subianto's legal director was quoted by Indonesian media as saying a formal appeal would be filed at the Constitutional Court.

Subianto unsuccessfully challenged Widodo's 2014 victory over him at the same court.

'Sway public opinion'

Analysts and election officials have discounted Subianto's claims that the result was affected by voter fraud and widespread cheating.

"The scale of abuses and errors in the conduct of the election are clearly very minor overall," Jakarta-based political analyst Kevin O'Rourke said before Tuesday's official results.

"(Subianto) and his camp are harping on minor deficiencies and very micro problems, clearly in an attempt to sway public opinion and delegitimise Widodo."

It was unclear if huge crowds would flood the streets to support Subianto, but there was little sign of Tuesday.

"There will be protests from disappointed (Subianto) supporters but I don't think it'll be significant," said Syamsuddin Haris, a political analyst at the Indonesian Sciences Institute.

"Authorities have been anticipating this," he added.

The presidential campaign was punctuated by bitter mudslinging and a slew of fake news online -- much of it directed at the presidential contenders.

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.

Online, Subianto was pilloried by social media users who poked fun at his repudiation of the unofficial results and for kissing the ground as he declared himself president.

Last month, a record 245,000 candidates ran for public office in Indonesia, 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 in total, it added.

Widodo scoring thumping victories in holiday hotspot Bali and heavily populated East Java, while Subianto landed big wins in conservative Aceh and West Java.