The youth had been detained on September 6 in the Mediterranean resort area of Sousse for questioning in connection with a murder after his telephone number was found on the victim, lawyer Fadoua Braham said.
He denied any involvement in the murder but admitted to having had sexual relations with the victim.
"Another statement was drawn up and my client had to undergo an anal exam against his will," she said.
The youth was sentenced Tuesday, Fadoua added, but the court could not be reached for comment.
Signup to our newsletter and follow us on Facebook and Twitter!
The young man told AFP through the lawyer: "I don't understand why I was sentenced... nor why I was detained for six days without being allowed to contact my lawyer."
"I want to get out and resume a normal life," he said. "I wonder what I'm going to do about my job and studies. I don't want to be rejected by society."
Local rights groups ATSM and Shams condemned the judgement, calling anal exams "scandalous."
They call for decriminalising homosexuality in Tunisia by revising Article 230 of the penal code, according to which sodomy between consenting adults is punishable by up to three years in prison.
In July, a Swedish man was given two years behind bars in Tunisia for "homosexual acts."