He was charged in an Islamic court in 2019 with attempting to have "intercourse against the order of nature", and several others in the same case have already pleaded guilty and were caned as a punishment.
Critics say the climate is worsening for the gay community in Muslim-majority Malaysia, with government officials often speaking out against LGBT people.
In Thursday's landmark case, the legislation banning gay sex was enacted in Selangor state, outside Kuala Lumpur.
Multi-ethnic Malaysia has a dual-track legal system, with Islamic courts handling some matters for Muslim citizens, and sharia laws set by individual states.
But local laws cannot conflict with legislation at the federal level, and sodomy is already a crime under the national penal code - although the statute is rarely enforced.
In its ruling, Malaysia's top court sided with the man who brought the case, who was not identified, saying Selangor state was not empowered to make such a law.
The ruling means that the law is overturned and the man's case should be dropped, according to his lawyer Surendra Ananth.
Gay rights activist Numan Afifi welcomed the "historic development".
"It marks monumental progress for LGBT rights in Malaysia," he said. "We have worked hard for so many years to live in dignity without fear of prosecution."
Despite the victory, Islamic laws banning gay sex still exist in some other states.
The man was among 11 arrested for allegedly having sex at an apartment in 2018. Some have admitted to the offence before an Islamic court and received six strokes of the cane, a fine and jail terms of up to seven months.
In another high-profile case, two women were caned in a sharia court in 2018 after being found guilty of having sex in Terengganu state.
About 60 percent of Malaysia's population is Muslim.