A high court in India's Karnataka state has ruled that the hijab is not "essential" to Islam in a landmark case that could have implications across the country.
The court also upheld a state government order that had banned headscarves in classrooms.
The verdict follows a months-long, divisive row over the hijab.
A Karnataka college's decision in January to bar entry to Muslim girls wearing the hijab had sparked protests.
The issue soon snowballed, forcing the state to shut schools and colleges for several days.
The matter reached the high court after some Muslim women protesters filed petitions arguing that India's constitution guaranteed them the right to wear headscarves.
The three-judge bench held that allowing Muslim women to wear the hijab in classrooms would hinder their emancipation and go against the constitutional spirit of "positive secularism".
The 129-page order quotes passages from the Quran and books on Islam to argue that the hijab is not an obligatory religious practice.
"There is sufficient intrinsic material within the scripture itself to support the view that wearing hijab has been only recommendatory, if at all it is. What is not religiously made obligatory therefore cannot be made a quintessential aspect of the religion through public agitations or by the passionate arguments in court,'' the order says.
The petitioners had argued that a February order by the government prescribing uniforms in educational institutions violated their constitutional rights.
The court, however, said the order was valid, holding that the government had the right to prescribe uniforms for students.
The judgment is likely to be appealed against in the Supreme Court.
"This is a pre-eminently fit case to go before the Supreme Court," Prof Ravi Varma Kumar, a senior advocate who appeared for one of the petitioners, told BBC Hindi.
Aliya Assadi, one of the petitioners, told reporters that she felt let down by the verdict.
"We had so much hope in our judicial system and our constitutional values. We feel we have been betrayed by our own country," she said.
Another petitioner, Almas AH, added that they will take the fight to the top court.
"I am not going to go to college without my hijab. And I will fight for it because the hijab is an essential part of my religion," she said.
The Karnataka government has welcomed the verdict.
Thousands of people in India - where Muslims are a minority - had avidly followed the court hearings.
While hearing the case, the bench had passed a contentious interim order that said students couldn't wear religious clothing - including the hijab - until it pronounced a verdict.
This led several Muslim women to skip classes and even their exams while the case was being heard. The row also polarised opinions, with critics seeing it as yet another attempt at marginalising Muslims by Prime Minister Narendra Modi's Hindu nationalist government.
The row started in in Karnataka's Udupi district after a government-run college barred six teenage students from wearing the hijab in class.
The college said it allowed students to wear the hijab on campus and only required them to remove it inside the classroom. But the girls, who all wore the mandatory college uniform, started a strike outside the college, arguing that they should be allowed to cover their hair in the classroom.
Udupi is one of three districts in Karnataka's communally sensitive coastal region - a stronghold of Mr Modi's right-wing Bharatiya Janata Party (BJP).
The protests soon spread to other colleges in the state, which is also governed by the BJP. Some Hindu students began going to classes wearing saffron shawls - the colour is seen as a Hindu symbol - which forced officials to insist that both could not be allowed on campus.
The issue attracted international attention after some demonstrations spiralled into violence - Nobel Prize-winner Malala Yousafzai tweeted about the issue, asking India's leaders to do something to "stop the marginalisation of Muslim women".
Reporting by Imran Qureshi, BBC Hindi