A massive demonstration by tens of thousands of Indonesian Muslims against Jakarta’s governor turned ugly Friday as hardliners burned police cars and clashed with officers, who responded with tear gas and water cannon.

The ugly scenes — just metres from the presidential palace — marred an otherwise peaceful rally against governor Basuki Tjahaja Purnama, a Christian accused of insulting Islam.

Police had earlier declared the much-hyped demonstration against Purnama, in which 50,000 protesters gathered at the city’s largest mosque before taking to the streets in a huge show of force — a largely peaceful affair.

But as night fell thousands of radicals turned violent, setting police cars ablaze and attacking officers who hit back with tear gas, water cannon and truncheons.

Jagalah hatimu agar tak terbakar amarah, tetap junjung persatuan di negeri tercinta ini, ayo terus serukan perdamaian, #JakartaHarusDamai! pic.twitter.com/DA029mhu9T — detikcom (@detikcom) November 4, 2016

Authorities took no chances in the lead up to the protest, deploying 18,000 officers and extra soldiers across Jakarta amid fears that radical elements could infiltrate the march.

The demonstration appeared to be dying down by dusk as thousands began leaving the protest zone around city hall, the presidential palace and national monument.

But by nightfall riot police were put to the test as mobs of hardliners, draped in the white militant uniforms favoured by Indonesian extremist groups, ran amok, hurling bottles, stones and lighting fires as officers used shields for cover.

Some public structures broken by protesters near the Palace #AksiDamai411 pic.twitter.com/EIOx6ymZsq — Natashya Gutierrez (@natashya_g) November 4, 2016

Security min Wiranto went to see leader of protest, but cancelled last mnt cos he couldn’t stand the tear gas: Detik https://t.co/aj22pI8W7o — Olivia Rondonuwu (@OliveRondonuwu) November 4, 2016

Massa #aksidamai411 membakar sampah di tengah Jl. Medan Merdeka Barat. Seorang demonstran membantah adanya pembakaran mobil | @santidewi888 pic.twitter.com/FEZw9tc2ng — Rappler Indonesia (@RapplerID) November 4, 2016

Massa mulai ngejarah Indomart di gedong panjang #safetycheckjkt pic.twitter.com/qWj4ks6v9P — Herna Cartriene (@hernacartriene) November 4, 2016

The protest was triggered by accusations that Purnama, better known by his nickname Ahok, insulted Islam by criticising opponents who used Koranic references to attack him ahead of an election in February.

Purnama apologised for the remarks, but his opponents have built a groundswell of support calling for his arrest and incarceration under Indonesia’s tough blasphemy laws.

“It’s no wonder people arise. Why when it comes to Ahok is the law not upheld?” deputy house speaker Fahri Hamzah, a prominent politician from an Islamic political party, told demonstrators earlier Friday.

Anger at Purnama, Jakarta’s second Christian governor and the first from the country’s ethnic Chinese community, spread beyond the capital, with solidarity marches also held across Java and in cities as far away as Makassar in Indonesia’s east.

The military warned it was ready to back police if things turned ugly, with helicopters flying low over the city and extra soldiers stationed at key government buildings reinforced with razor wire and armoured vehicles.

Some foreign embassies warned their citizens to steer clear of the demonstration.

President Joko Widodo met this week with religious and political leaders to issue a unified call against violence while police sought to ease tensions by holding prayer sessions and broadcasting calls for peace on social media.

Indonesia is home to the world’s biggest Muslim population, where a vast majority practise a moderate form of Islam.

But the governor stoked religious tensions in September when he told a crowd they’d been “deceived” by his opponents who had used a Koranic verse to try to put them off voting for a Christian.

The governor — known for his tough-talking style — is hugely popular in other quarters for his determination to clean up Jakarta, an overcrowded, disorganised and polluted metropolis.

Purnama became Jakarta governor in November 2014, but was not elected to the post. He was deputy governor and automatically became governor after incumbent Widodo was elected Indonesian president.

Grove: Coconuts Brand Studio

Fast. Funny. Digital. We produce creativity that delights and influences customers. Join forces with us to slay buzzwords, rise above the noise, and sow the seeds of something great.