BANGKOK — It began with a political jest and culminated in a shocking prison sentence.

On Thursday, Basuki Tjahaja Purnama, a former governor of the Indonesian capital, Jakarta, was released from prison after serving nearly two years for blasphemy against Islam.

An ethnic Chinese Christian in a country with the world’s largest Muslim population, Mr. Basuki, 52, ran afoul of Indonesia’s blasphemy law when he tried to counter suggestions that faithful Muslims should not support non-Muslim politicians.

As Muslim supporters cheered him on during a public event in 2016, Mr. Basuki said in a joking manner that a particular verse in the Quran was being misused to dissuade Muslims from voting for him.

The off-the-cuff comment, which was later edited online to sound dismissive of the Muslim holy book, incensed hard-line Muslim groups, some of which have called for an Islamic caliphate to replace Indonesia’s secular democracy.