Richard C. Paddockmay, Jakarta, Indonesia – The popular governor was the clear front-runner. Despite his religion, a Christian in Muslim-majority Indonesia, Gov. Basuki Tjahaja Purnama of Jakarta held a commanding lead in the polls.
Then, in a campaign speech in September, he made a fatal reference to a verse of the Quran. A video, edited to make it seem that he had said the Muslim holy book was misleading people, went viral on Facebook.
Mr. Basuki was charged with blasphemy, mass rallies were mobilized against him and despite a tearful apology, he was trounced in a runoff election last month.
"A half-minute destroyed his career," said Komaruddin Hidayat, a former rector of Syarif Hidayatullah State Islamic University Jakarta.
The governor's loss was the most prominent sign yet of Indonesia's tilt toward political Islam. A moderate, secular democracy with the world's largest Muslim population, Indonesia in many ways provides a counterweight to the sectarian clashes and autocratic rule that have plagued Muslim countries in the Middle East, some 5,000 miles away.
But in recent years, the radical Muslims who have been trying to turn Indonesia into a strict Islamic state have gradually gained influence, accruing an array of significant victories.
As the Jakarta election was underway, the Constitutional Court issued a ruling that was less noticed but could have a broader effect. The court struck down a law allowing the government to annul discriminatory local laws, such as religious-based laws regulating morality or women's behavior.
Local laws are where the Islamists have made the biggest gains. Since 1998, with the introduction of democracy and the decentralization of power to the local authorities, more than 440 local ordinances have been adopted imposing elements of Islamic law, or Shariah, like requiring women to wear head scarves or restricting alcohol sales, according to Michael Buehler, a senior lecturer at the University of London's School of Oriental and African Studies, in his book "The Politics of Shari'a Law."
"Religion has become politicized in local elections, and we saw that emerge in a big way in the election for governor in Jakarta," said Melissa Crouch, a senior lecturer at the University of New South Wales in Sydney who researches Asian legal systems. "Democracy gives a greater space to everyone, including greater space for radical Islam."
While the ultimate goal of those promoting such laws is an Islamic state, that prospect seems distant. Indonesia remains mostly tolerant and moderate. In Jakarta, the capital, many Muslim women socialize freely with men, ride motorbikes and decline to wear head scarves. Islamist parties have not fared well in national elections.
But the Islamists have kept up the pressure on a variety of fronts. There have been efforts to change national laws: A bill before Parliament would ban alcohol nationwide, while the Constitutional Court is hearing a petition by an Islamist group demanding that gay sex be outlawed and that the adultery law be broadened to criminalize sex between any unmarried people.
The blasphemy law, rarely used before 2004, has been deployed in more than 120 cases, helping build support for the Islamists and silence dissent, said Andreas Harsono, the Indonesia representative for Human Rights Watch.
A self-appointed morality brigade called the Islamic Defenders Front, best known for smashing up bars, has recently attacked people for selling food on Ramadan or wearing Santa outfits at Christmas.
While such efforts by hard-core activist groups are not new, what stood out in the governor's election was their tacit acceptance by moderate Muslims, many of whom took offense at a perceived insult to their religion.
The radical groups organized the protests, demanding that the governor be jailed or killed and warning Muslims of dire consequences if they voted for a Christian. The sight of an estimated half million people at a rally in December was shocking in relatively cosmopolitan Jakarta even if, as has been reported, many protesters had been bused in from the more conservative hinterlands.
Neither of Mr. Basuki's rivals publicly questioned the blasphemy accusation, instead forging alliances of convenience with the radicals.
Anies Baswedan, the moderate former education minister who won the race, visited the headquarters of the Islamic Defenders Front during the campaign, addressing the group as its leader, Habib Rizieq, sat by his side.
Even President Joko Widodo, an ally of Mr. Basuki's, appeared unexpectedly at the December rally alongside Mr. Habib, thanking the crowd for holding a peaceful rally. Both appearances demonstrated how much influence the militants had gained during the election campaign.
One of the groups behind the rallies was Hizbut Tahrir, an organization dedicated to creating a state governed by a harsh form of Shariah, including stoning adulterers and amputating the hands of thieves, said Ismail Yusanto, a spokesman for the group in Indonesia.
"From the Islamic perspective, the state should exist for only one purpose," he said, "how to implement Shariah."
The group is banned in many Muslim-majority countries. "This is an ideology that has the potential to threaten democracy," said Yenny Wahid, director of the Wahid Institute, an Islamic research center founded by her father, the former president Abdurrahman Wahid.
She likened the appeal of these laws to the populist proposals offered by President Trump. "Build a wall. Chop off people's hands. These are very simplistic solutions to people's problems," she said.
Another faction behind the protests is the Salafists, a Saudi-led movement that seeks a return to the pure form of Islam said to have been practiced by Mohammed and the first generations of Muslims. Since the 1980s, Saudi Arabia has funded dozens of schools, scholarships, the distribution of religious material and the construction of mosques in Indonesia. The commitment has been relatively small given Indonesia's vast size, but influential in spreading a puritanical, Salafist way of thinking and an atmosphere of intolerance.
What the Islamists have in mind already exists in Aceh Province, on the northern tip of Sumatra. The province began instituting Shariah law in 2001 after gaining autonomy in an attempt to end a long-running separatist war.
Over the years, the law has become increasingly strict: Women are required to dress modestly, alcohol is mostly banned, and adultery and homosexuality are punishable by public caning. The morality police roam the province, scouring hotel rooms and beaches for immoral behavior and ordering people to go to mosques and pray.
"When Aceh was granted autonomy, it opened a Pandora's box for Indonesia," Mr. Harsono said. Some locals point out with pride that they have become a model for the rest of the country.
Indonesia also faces the threat of Islamic terrorism, as a suicide attack in Jakarta last year reminded the country.
Four civilians and four attackers were killed in the midday attack on a police post in a busy commercial district. The police said the attack had been organized by an Indonesian member of the Islamic State in Syria.
About 450 Indonesians who tried to join the Islamic State have returned home, most caught in Turkey or other countries before reaching Syria. Sidney Jones, director of the Institute for Policy Analysis of Conflict, said most of the returnees were noncombatants who did not pose a significant threat.
More worrisome, she said, are some two dozen Islamic State recruits who returned from Syria with training and combat experience.
Just as alarming, the institute has found that Jemaah Islamiyah, the regional terrorist group that carried out the 2002 Bali bombing, which killed more than 200 people, is regrouping. Some of its members have trained in Syria with non-Islamic State militias, the institute said in a report released last month.
In recent years, the police have kept the militants under tight control, apprehending or killing dozens and foiling planned terrorist attacks.
For the radical political groups, which profess nonviolence, the question is whether they can capitalize on their success to play a role in the 2019 presidential election.
In opposing Mr. Basuki, the Islamists were part of a broad coalition led by Prabowo Subianto, a former general who ran for president in 2014 and may run again. It also included Christians, among them Hary Tanoesoedibjo, a billionaire businessman who is Mr. Trump's business partner in Indonesia and has his own ambitions for national office.
For them, taking out Mr. Basuki was a way of weakening Mr. Joko heading into the presidential election, even if it meant allying themselves with hard-line Islamists. The winner, Mr. Anies, was the candidate of Mr. Prabowo's party.During a victory celebration the day after the election at Indonesia's national mosque, Mr. Prabowo publicly thanked leaders of the campaign, including Mr. Rizieq of the Islamic Defenders Front and Bachtiar Nasir, a Salafist leader.
Later, Mr. Prabowo dismissed concerns that he may have entered into an unholy alliance with the extremists. They were part of a "grand coalition," he said.
"If we demonize and we consider certain groups to be not qualified to join the political discourse," he said, "that is not a healthy attitude."
[A version of this article appears in print on May 7, 2017, on Page A6 of the New York edition with the headline: Governor's Defeat In Jakarta Shows Rise of Islamists.]