India's Supreme Court has said women can no longer be barred from entering the Sabarimala temple, considered to be one of the holiest for Hindus.
The temple in Kerala barred women of a "menstruating age" - defined as between the ages of 10 and 50 - from entering.
Menstruating women are not allowed to participate in religious rituals or enter temples, as they are considered "unclean" in Hinduism.
The ruling came after a petition argued the custom violated gender equality.
While most Hindu temples allow women to enter as long as they are not menstruating, the Sabarimala temple is unusual in that it is one of a few temples that does not allow women in the broad age group to enter at all.
Millions of devotees visit Sabarimala every year.
In the judgment Chief Justice Dipak Misra said that "religion is for one dignity and identity", adding that "the right to practise religion is available to both men and women".
The impending retirement of Justice Misra has seen a flurry of historic liberal rulings from the court in recent days, including the striking down of colonial-era laws that criminalised adultery and gay sex.
Justice Misra, who will retire on Tuesday, was heading a five-judge bench which gave a 4-1 verdict.
The BBC's Soutik Biswas says such a stream of judgements leading up to a judge's retirement in the top court is not unusual.
"But what it also underlines is that the 25 judges of the court are some of the most overworked in the world - one study found that a single Supreme Court judge, during his tenure over four to six years, hears some 6,000 cases alone," our correspondent adds.
Indu Malhotra, the only woman judge on the bench, dissented with the majority verdict.
"Issues of deep religious sentiments should not be ordinarily interfered by the court... Notions of rationality cannot be invoked in matters of religion," she said in her dissenting opinion.
'God does not discriminate'
Analysis by Geeta Pandey, BBC News
For centuries, temples and shrines have cited tradition to keep women out and their managements, dominated mostly by patriarchal men, have used menstruation to keep female devotees away.
But in recent years, they have faced an unprecedented challenge from women's groups.
Friday's order, which accepts womens' right to worship at Sabarimala, is expected to help remove some of the stigma associated with periods.
The order has not come as a surprise - Chief Justice Dipak Misra had previously questioned the validity of the practice, saying that since God does not discriminate between genders, who are we to do that?
Also, in the past two years, courts have unlocked the gates of Shani Shingnapur temple and Haji Ali mosque for women.
Three years ago, the Sabarimala temple chief said he would allow women to enter the shrine only after a scanner was invented to detect if they were "pure"- meaning they weren't menstruating.
Today, the judges have told him that we won't be needing that scanner anymore.
The state government of Kerala had opposed the entry of women when the case was first taken up in 2016. However it changed its stance in a recent hearing to support the petitioners instead.
At a hearing in July, petitioners argued that this custom violated equality guaranteed under India's constitution. They added that it was prejudiced against women and their right to worship.
But supporters of the ban argued that the practice had been in effect for centuries, and there was no need to change it now.
The campaign to repeal the ban on women entering the temple gathered momentum in 2016 after a protest by female students.
One of the protesters also started a #HappyToBleed campaign on Facebook against "sexist attitudes", which received support from different parts of the country.
Nikita Azad, who started the campaign, told the BBC this is a historic judgement. "It will have a large impact since the Supreme Court has destigmatised menstruation and upheld equality over religion," she said.
This is the third religious site in India where women have gained the right to enter through judicial intervention. Courts directed authorities of the Hindu temple Shani Shingapur and the Muslim Haji Ali shrine, both in the western state of Maharashtra, to allow women inside.
What is the significance of the Sabarimala temple?
Sabarimala is one of the most prominent Hindu temples in the country. Millions of devotees from all over the world visit the temple to seek blessings.
To enter the temple, pilgrims have to climb 18 holy steps. According to the temple's website, the act of crossing these 18 steps is so sacred that no pilgrim can climb them without undertaking a rigorous 41-day fast.
Devotees are also supposed to follow specific rituals before they enter the shrine.
Some of them include pilgrims wearing only black or blue and not being allowed to shave until the completion of their pilgrimage.
As part of the ritual, they also smear sandalwood paste on their foreheads.