The result was released a day ahead of schedule in the early hours of Tuesday, amid fears of violent unrest.
Around 32,000 security personnel were deployed across the capital Jakarta, AFP news agency reports.
Mr Prabowo, 67, has not yet confirmed if he will challenge the result in court.
Ahead of the final tally he had alleged "widespread cheating" and warned of potential street protests. In 2014 Mr Prabowo lost a court challenge following an election defeat by Mr Widodo.
The latest vote followed a bitter campaign in which religion played a key role, but independent observers have said it was free and fair.
Mr Widodo won 55.5% of the vote to Mr Prabowo's 44.5%, the election commission said. More than 192 million people were eligible to vote in the 17 April poll, choosing 20,000 local and national lawmakers.
Azis Subekti, a witness from the retired general's campaign team, refused to sign the results.
"We won't give up in the face of this injustice, cheating, lies, and these actions against democracy," he said.
Joko Widodo has been president since 2014 when he also beat Mr Prabowo in a hotly-contested vote.
The 57-year old comes from humble beginnings and has focused his campaigns very much around his image as a "man of the people".
He first came to international prominence by becoming governor of Jakarta in 2012.
Under his presidency, Indonesia's economy has grown steadily.
Yet he has disappointed some supporters by abandoning campaign promises to resolve human rights violations. He has also courted massive Chinese infrastructure investment, which some in the country view as controversial.
His political rival, Mr Prabowo, is closely associated with Indonesia's traditional political elite.
He was previously married to the daughter of former dictator General Suharto, who ruled Indonesia with an iron fist for 30 years.
More about: Indonesia