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, with some campaigners kissing, while others applauded or waved the rainbow flag — a symbol of gay rights.
“We are making history so that people may know who they are and express their feelings, express love because God talks about love,” an ecstatic Thabo Otukile told AFP outside the court.
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.”
Activists had launched the legal battle after the Home Affairs ministry rejected an application to register the Lesbians, Gays, and Bisexuals of Botswana (LEGABIBO) organisation.
The main applicant, identified only by initials LM for security reasons, challenged sections 164 and 167 of the penal code.
LEGABIBO chief Anna Mmolai-Chalmers was overwhelmed by the outcome saying it was the culmination of “twenty years hard work” and signalled “the end of the exclusion”.
“For a lot of us it has not sunk in,” she said.
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.”
Anneke Meerkotter, of the Southern Africa Litigation Centre (SALC), described the judgement as “groundbreaking”, adding that it allows LGBT rights to be protected under the charter.
“Culture cannot be excused to violate constitutional rights. The judgment showed the world how it can be done,” she said.
27 African countries opposed to gay rights and same sex relationships:
Nigeria, Kenya, Mauritania, Sudan , Somalia, Senegal, Gambia, Guinea.Sierra Leone, Liberia, Ghana, Togo, Cameroon, Chad, South Sudan, Uganda, Burundi, Namibia, Zambia, ESwatini, Zimbabwe, Malawi, Ethiopia, Eritrea, Mauritius