The ruling, which drew mixed reactions in the socially conservative country, was in sharp contrast to developments in Uganda, where one of the world's most draconian anti-LGBT laws is expected to come into force imminently.
The Namibian case stemmed from the residency applications of a German woman who married a Namibian woman in Germany, and of a South African man who married a Namibian man in South Africa, the only country on the continent that allows same-sex marriage.
The government refused to give the non-Namibian spouses residency rights in the country, on the grounds that their marriages could not be recognised in Namibia, which prompted them to take legal action.
"The approach of the (interior) ministry to exclude spouses ... in a validly concluded same-sex marriage ... infringes both their interrelated rights to dignity and equality," the Supreme Court said.
A spokesperson for the interior ministry did not immediately respond to a request for comment.
Gay rights activist Linda Baumann welcomed the ruling as a step in the right direction.
"Today's verdict and outcome clearly indicates that Namibia is moving towards recognising diversity in this country irrespective of people's political or social positioning," she said.
But an opposition political party, Namibia Economic Freedom Fighters, said the Supreme Court was forcing foreign cultural views on Namibians - echoing arguments raised by other African opponents of LGBT rights who call them "un-African".
Sexual contact between men is a criminal offence in Namibia, though the law is seldom enforced.
Like Namibia, many other African nations still ban same-sex relationships, with couples risking jail and public scorn.
Reuters, the news and media division of Thomson Reuters, is the world's largest multimedia news provider, reaching billions of people worldwide every day.Go to: https://www.reuters.com/