A court in Tunisia has sentenced six students to three years in prison each on charges of homosexuality in a judgment condemned by rights activists.

The court in Kairouan handed down the maximum sentence last week under a controversial article of the criminal code that criminalises sex between men, their lawyer, Boutheina Karkni, said on Monday.

The six were also banned from the city for five years after they complete their sentences, she said.

The young men were detained in November and December and made to undergo anal examinations after “neighbours denounced them”, she said.

Karkni described the ruling as “extremely harsh” and said an appeal hearing should take place in two to three weeks in the nearby city of Sousse.

One defendant was handed an extra six months in prison for “offensive” videos found on his computer, the news site huffpostmaghreb.com reported.

The human rights group Amnesty International denounced the ruling as “a shocking example of deep-rooted state sanctioned discrimination”.

It said anal examinations “amount to torture when carried out involuntarily”, and called for the immediate and unconditional release of the six.

Shams, a local gay rights group that calls for decriminalising homosexuality in Tunisia by revising article 230 of the penal code, condemned the judgment.

Human Rights Watch’s country director also denounced the ruling as “a grave case of infringement on people’s private lives and physical integrity”. Amna Guellali described the sentences, which included the “extremely rare” penalty of banishment, as “medieval and not consistent with Tunisia’s evolution”.

Balkis Mechri, of the local Human Rights League, agreed. “The banishment really is scandalous,” she said, calling on civil society to “react strongly”.

An interior ministry spokesman, Walid Louguini, defended the ruling. “Our job is to uphold the law,” radio station Shems FM reported him as saying.

In September, a court sentenced a student to a year in prison in Sousse on charges of homosexuality, also after he was examined anally. He was released in November pending an appeal hearing expected on Thursday.

After the September judgment, the then justice minister, Salah Ben Aissa, called for article 230 to be scrapped and was sacked in October.