Botswana's High Court ruled Tuesday in favour of decriminalising homosexuality, handing down a landmark verdict greeted with joy by gay rights campaigners.
Under the country's 1965 penal code, homosexuality is punishable by a jail term of up to seven years.
But Judge Michael Elburu declared it was time to "set aside" the "provisions of a Victorian era" and ordered the laws be amended.
In a courtroom packed with activists, the judge declared that the current laws oppressed a minority of the population.
"There's nothing reasonable in discriminating," he said.
"We say the time has come that private, same sexuality must be decriminalised."
"It is a variety of human sexuality," he said.
Jubilation erupted in the courtroom as the decision was announced, and campaigners waved the rainbow flag of gay rights.
In Geneva, the UN agency UNAIDS added to the applause.
"This is a historic ruling for lesbian, gay, bisexual and transgender people in Botswana," Gunilla Carlsson, UNAIDS' executive director, said in a statement.
"It restores privacy, respect and dignity to the country's LGBT people, and it is a day to celebrate pride, compassion and love."
"Botswana's ruling today to decriminalize #homosexual sex is a refreshing nod to human dignity, privacy, and equality," Neela Ghoshal, a specialist at Human Rights Watch (HRW), said in a tweet.
The High Court had been petitioned by an anonymous person, identified only by initials LM for security reasons, who had challenged sections 164 and 167 of the penal code.
In March, the court postponed a ruling on the issue, sparking fears that the much-awaited decision could be delayed indefinitely.
But on Tuesday, Judge Elburu stressed that the country's highest judicial body took the matter deeply seriously.
"Sexual orientation is human, it's not a question of fashion," he said. "The question of private morality should not be the concerns of the law."
Last month, Kenya's High Court upheld laws against same-sex relations, dealing a blow to activists campaigning to roll back anti-gay laws and stigma which are widespread in Africa.
At present 28 out of 49 countries in sub-Saharan Africa, including Botswana, have laws penalising same-sex relationships, according to HRW.
The death penalty is on the books, under sharia, in Mauritania, Sudan and northern Nigeria, although there have been no known executions in recent times.
In southern Somalia, gay men are believed to have been put to death in territory ruled by the Al Shabaab jihadist group.
However, Angola, Mozambique and Seychelles have scrapped anti-gay laws in recent years.
Rights groups say many laws punishing homosexuality in Africa date from the colonial area.
They represent a peril even in countries where they are not implemented, according to campaigners, as their existence on the statute books entrenches discrimination and encourages harassment.
UNAIDS said decriminalisation would help the fight against AIDS.
The large southern African country has a population of only 2.3 million, but is struggling with one of the highest rates of HIV infection in the world.
According to UNAIDS figures, 22.8 percent of adults aged between 15 and 49 in Botswana are living with the AIDS virus.
Outreach worker Thato Game Tsie said scrapping the anti-gay laws would help the community access health care and treatment more easily.
"There are many services we require as gay men that some nurses are not aware of, and if we go to a government hospital there will be those negative comments said to you," Game Tsie told AFP.
"So we just want to be free to access these services."
Legal and political steps in favour of liberalisation had come before Tuesday's historic decision.
In 2016 the country's appeals court ruled that the government was wrong to refuse to register an organisation representing homosexuals and other minority sexual groups.
Last December, President Mokgweetsi Masisi addressed a meeting on gender-based violence, saying there are "many people of same sex relationships in this country who have been violated and have also suffered in silence".
"Just like other citizens, they deserve to have their rights protected," he said.
At the High Court hearing in Gaborone in March, lawyers for the applicant said public opinion on same-sex relationships had evolved.
Employment legislation in Botswana now outlawed discrimination on grounds of sexual orientation, which made the contested sections of the penal code out of date, they argued.