Religious police caned two men for gay sex, with hooded men inflicting 82 lashes on each of them as hundreds of people watched the punishment ordered by an Islamic court in Aceh province, the only part of Indonesia that criminalises same-sex relations and that uses Islamic law as its legal code.

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

The crowd shouted insults and cheered as the men, aged 20 and 23, were whipped across the back. Many of the spectators filmed the caning with mobile phones as a team of five robed and hooded enforcers took turns to inflict the punishment, relieving one another after every 20 strokes for one of the men and 40 for the other. Many people watched the punishment being meted out on a livestream video.

Sarojini Mutia Irfan, a female university student who witnessed the caning, told AP it was a necessary deterrent. "What they have done is like a virus that can harm people's morale," she said. "This kind of public punishment is an attempt to stop the spread of the virus to other communities in Aceh."

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

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 signalled they were in too much pain. At the end of the lashing, the two men were helped off the platform. They were free to return to their families, authorities said.

At least eight other men and women were caned for other offences such as adultery.

gay couple caned Aceh Indonesia shariah
An Acehnese woman is caned in public for spending time with a man who is not her husband, which is against Sharia law Ulet Ifansasti/Getty Images

With the exception of Aceh, homosexuality is not illegal in Indonesia, but the country's low-profile LGBT community has been under siege in the past year.

Prejudice has been fanned by stridently anti-gay comments from politicians and Islamic hard-liners, and a case before the country's top court is seeking to criminalise gay sex and sex outside marriage. On Monday (22 May), 141 men were detained in a police raid on a gay sauna in Jakarta, the capital. Rights groups and activists have raised concern over the persecution of minorities, moral policing and violations of privacy and expression.