Mr Widodo, a former furniture exporter known to most as "Jokowi," is the first candidate in a direct presidential election with no ties to the former dictator Suharto, who ruled for 30 years before being overthrown in 1998.
The other contender, former general Prabowo Subianto, declared he was withdrawing from the contest shortly before the numbers were released by the Election Commission, saying there was massive fraud during the election and that it was unfair and undemocratic.
Mr Widodo had maintained a slim lead of about four percentage points in unofficial "quick counts" by polling agencies released after the July 9 election. But Mr Subianto, who has declared assets of $140 million and was on his third bid for the presidency, repeatedly claimed that polling firms with links to his campaign showed he was ahead.
"We reject the 2014 presidential election which is illegitimate and therefore we withdraw from the ongoing process," he said.
Observers of the July 9 election said they were generally fair and free, with minimal abnormalities. Maswadi Rauf, a political professor at the University of Indonesia, said he saw no sign of significant fraud, as alleged by Mr Subianto.
Mr Subianto's rejection of the results "reflects the real attitudes of the elite, who have not yet ready to accept losing," Rauf said. "We are still in transition to democracy, which is indeed not our culture. And what is happening indicates we still are immature, we need to learn."
There were no immediate reports of violence. About 100 Subianto supporters held a peaceful protest about 300 metres from the Election Commission building in downtown Jakarta, chanting "Prabowo is the real president" and holding banners saying that the commission should stop cheating.
The building was surrounded by thousands of policemen to maintain security after a particularly nasty presidential campaign marred by smear tactics from both camps. Mr Widodo blamed his drop in opinion polls in the weeks before the election on character assaults that accused him, among other things, of not being a follower of Islam - which he denounced.
Mr Subianto, meanwhile, came from a wealthy, well-known family. He had a dubious human rights record during his military career, but was seen as a strong and decisive leader. His campaign was better financed and he also got endorsements from most of the country's major political parties.
Despite Mr Widodo's lack of experience in national politics, he built a reputation as being a man of the people and an efficient leader who wants to advance democratic reforms and was elected to run the capital in 2012. He is widely viewed as untainted by the often corrupt military and business elite that has run Indonesia for decades.
Final results showed that Mr Widodo won 70,997,859 votes, or 53.15% of the nearly 133 million valid ballots cast, while Mr Subianto won 62,576,444 votes, or 46.85%.
Voter turnout topped 70%. The commission was to formally declare the winner later Tuesday evening.