JAKARTA, Indonesia — President Joko Widodo of Indonesia led by a comfortable margin in unofficial returns in his re-election bid on Wednesday, as he appeared to fend off a challenge by a four-time presidential candidate supported by hard-line Islamists.

Mr. Joko, who is seeking a second five-year term, has made expanding social programs and building roads, airports, seaports and transit lines high priorities, and his approach appeared to be paying off with voters across the sprawling archipelago.

His opponent, Prabowo Subianto, tried to win popular support by attacking “evil elites” who he said had undermined the country. Mr. Prabowo was once married to a daughter of Suharto, the dictator who ruled Indonesia for three decades up to 1998, and he was dismissed from the army decades ago for ordering his troops to kidnap activists.

Unofficial vote counts had Mr. Joko leading by roughly 10 percentage points. Official ballot counts in the far-flung island nation take weeks, but the winner usually becomes apparent hours after the voting through so-called quick counts, in which independent polling companies tally ballots from a sampling of polling places nationwide.