Botswana's High Court has ruled in favour of decriminalising homosexuality in a landmark decision for campaigners.
The court rejected laws that impose up to seven years in prison for same-sex relationships, stating they were unconstitutional.
The move contrasts with Kenya's recent ruling against campaigners seeking to overturn laws on gay sex.
"Human dignity is harmed when minority groups are marginalized," Judge Michael Elburu said.
Three judges came to the decision unanimously.
Judge Elburu labelled laws banning gay sex as "discriminatory" and added: "Sexual orientation is not a fashion statement. It is an important attribute of one's personality."
The law has been in place since 1965 when it was brought in by the colonial British government.
The case was brought to court by a student who argued society had changed and homosexuality was more widely accepted.
Activists welcomed the decision and described it as a significant step for gay rights on the African continent.
Laws outlawing same sex relations exist in 31 out of 54 African countries, according to the International Lesbian, Gay, Bisexual, Trans and Intersex Association (ILGA).
Gay sex can be punishable by death in northern Nigeria, Sudan, Somalia and Mauritania. Tanzanian laws mean homosexuality can result in a life sentence.
Angola, Mozambique and the Seychelles have all scrapped anti-homosexuality laws in recent years.
On 24 May, Kenya's High Court ruled against overturning a law banning gay sex.