The court ordered that sexual minorities should be guaranteed the same rights as other citizens.
Campaigners said the ruling was a "huge victory". Homosexuality is frowned upon in conservative South Asia.
Nepalese laws do not explicitly criminalise homosexuality, but an "unnatural sex act" currently carries a prison term of up to a year.
Human rights campaigners say the provision has been used to justify arrests of men who have sex with men and transgender people.
Gay men and women and members of other sexual minorities have long complained of discrimination in Nepal.
In their ruling, two Supreme Court judges said: "The government of Nepal should formulate new laws and amend existing laws in order to safeguard the rights of these people.
"Lesbian, gay, bisexual, transsexual and intersex are natural persons irrespective of their masculine and feminine gender and they have the right to exercise their rights and live an independent life in society."
Activists said it was a landmark ruling.
"It's a very encouraging and progressive decision. We all feel we are liberated today," Sunil Babu Pant, the president of the Blue Diamond Society which campaigns for Nepal's sexual minorities, told the AFP news agency.
"There were no specific laws to protect the rights of sexual minorities but the Supreme Court's decision has opened the doors to enjoy our rights."
Mr Pant said education, citizenship papers and jobs could now be given to people without them having to identify themselves as male or female, or giving their gender as "third sex".
There was no immediate response from the government to Friday's ruling.