NEW DELHI — In a landmark ruling on Tuesday, the Supreme Court of India allowed transgender people to identify as a third gender and directed the central and state governments to give full legal recognition to them.

The top court was responding to a public interest lawsuit filed by the National Legal Services Authority, which provides free legal services to the poor and disadvantaged. The group had argued that treating transgender people as legal nonentities, unable to apply for official identification documents, meant that they were deprived of basic human rights.

The court agreed with the organization and directed governments to treat the transgender community as a minority group that is eligible for welfare programs, health care and employment and help transgender people to become part of mainstream society.

For centuries, the transgender community has occupied a singular place in Indian culture. In the past, many were eunuchs, called hijras, a Hindi term for belonging to neither gender, who served as sexless watchdogs of Mughal harems. Today, many people who identify as transgender, even the many who are not castrated, are regarded with suspicion and ridicule, and are thought to possess powers of sorcery by some.

Activists who work with the community were overjoyed with the Supreme Court’s ruling.

“I was just talking to my mom about this decision, and I told her, ‘Mom, I’m legal,’ ” said Abhina Aher, 39, who identifies as transgender and works at the nonprofit HIV/AIDS Alliance. “‘You don’t have to be ashamed of me anymore.’”

“I am so happy,” said Anjali Gopalan, founder and executive director of the Naz Foundation, a nonprofit group that has been working in the fields of H.I.V./AIDS awareness and transgender rights.

“It is a progressive judgment, and it has a far-reaching consensus,” Ms. Gopalan added.

Colin Gonsalves, a lawyer who has been fighting cases for the transgender community, called the judgment “extraordinary.”

“It comes after decades of pursuit,” Mr. Gonsalves said. “It is the first step toward recognizing the transgender community as a third sex.”

Mr. Gonsalves added that this judgment was like a “breath of fresh air” after the Supreme Court’s ruling in December that criminalized gay sex in restoring Section 377 of the Indian Penal Code, which had been been struck down by a lower court.

For some transgender Indians, the Supreme Court ruling on Tuesday was not a complete salve for a life on the margins. Shalini Nair, a 29-year-old who lives in Mumbai, said that until two years ago she had to beg on the streets, which she called “collecting.” Now, she makes a living dancing at weddings, where the presence of transgender people is thought to be auspicious.

Though she identifies as transgender, she thinks of herself as a woman, and though encouraged by the possibility of health and education benefits for her community, she was ambivalent about the third gender status.

“It is a sign of separation,” she said. “Transgender people should be free to choose their own sex.”