Religious police in Indonesia caned two men for gay sex on Tuesday, with hooded men inflicting 82 lashes on each of them as hundreds of people watched the punishment ordered by an Islamic court.

Cheers and applause went up from a crowd gathered outside a mosque in the city of Banda Aceh, capital of the conservative province of Aceh, as the masked men took turns to flog the pair on a raised platform.

The men stood quietly, their heads down, as spectators heckled them and shouted insults.

An Indonesian man is publicly caned on May 23, 2017, for having gay sex in Banda Aceh, Indonesia. Beawiharta / Reuters

The punishment, condemned by rights groups, marked the first application of anti-homosexuality laws introduced in the province in 2014.

Aceh has long had a reputation as Indonesia's most conservative region. It is the only province that criminalizes same-sex relations and that uses Islamic law as its legal code in addition to the national criminal code.

Up to 1,000 people, many filming with smartphones, watched as the two men received 82 lashes each. Many others watched the punishment being meted out on a livestream video.

Some in the crowd carried banners rejecting the presence of the lesbian, gay, bisexual and transgender community in Aceh.

Related: Indonesian Police Detain 141 Men in Gay Club Raid

The Morning Rundown Get a head start on the morning's top stories. This site is protected by recaptcha

"I hope there won't be any more such cases of homosexuality, it is shocking," said Eni Tri Retnaningsih, a 20-year-old student who said she had watched others caned before for other offences such as adultery.

At least eight other men and women were caned for other offenses ahead of the punishment of the two found guilty of having gay sex.

Police separated men and women in the crowd as proceedings began, with 10 men in brown robes and hoods preparing a bundle of canes in case any broke during the flogging.

The beatings were delivered on the clothed backs of the men and women, some of whom cried out in pain with each stroke. In some cases, the caning was suspended briefly if the offender signaled they were in too much pain.

An Indonesian man, one of two to be publicly caned for having gay sex, is taken to be caned in Banda Aceh on May 23. Chaideer Mahyuddin / AFP - Getty Images

At the end of the lashing, the two men were helped off the platform. They were free to return to their families, authorities said.

Aceh is Indonesia's most westerly province, on the northern tip of Sumatra island, where Islam first took hold in the archipelago.

The province caned 339 people in 2016 for a range of crimes, according to Human Rights Watch.

Indonesia has the world's largest population of Muslims but is officially a secular country. Most Muslims practice a moderate form of the religion.

Religious police in Aceh have also been known to rebuke Muslim women without headscarves or those wearing tight clothes, and people drinking alcohol or gambling.

Related: Indonesian University Tries to Weed Out LGBTQ Applicants

Aceh is the only province allowed to implement sharia Islamic law under a special agreement that was signed in 2005 to bring an end to a bloody separatist movement.

Homosexuality is not illegal under Indonesian national law, but the LGBTQ community has come under pressure since government officials expressed reservations last year about activism by its members.

Police in the capital, Jakarta, detained more than 140 men in a raid on a gay club on Sunday on suspicion of violating pornography laws.

Rights groups and activists have raised concern over the persecution of minorities, moral policing and violations of privacy and expression.

A presidential spokesman declined to comment on the caning.

Follow NBC Out on Twitter, Facebook and Instagram