A small municipality in the Canadian province of Quebec that was facing a million-dollar lawsuit from an oil and gas exploration company has won its court battle, bringing an end to a four-year ordeal that began when residents took steps to protect their water supply.

“Reason and law prevailed today,” François Boulay, the mayor of Ristigouche Sud-Est, a township of 157 people on Quebec’s Gaspé Peninsula, said in a statement. “We are relieved that our right to protect our drinking water is finally recognised.”

The clash traces its roots to 2011, when the province granted a Montreal-based company, Gastem, drilling permits to search for oil and gas in the eastern part of the province. Construction began on a drilling platform in the township’s territory.

Amid concerns from Ristigouche Sud-Est residents over how the drilling would affect municipal water sources, the town passed a bylaw in 2013 that set out a 2km (1.2-mile) no-drill zone around its water supply.

Gastem shot back with a lawsuit that claimed residents had created an illegal bylaw to prevent the project from moving forward. The company’s initial C$1.5m ($1.2m) claim for damages was later reduced to C$984,676 – a figure that was more than three times the township’s annual budget.

After years of mounting anxiety among residents, a judge at the superior court of Quebec ruled this week that Ristigouche Sud-Est was within its rights to protect its water supply.

“Far from being adopted in an untimely and hasty manner, the bylaw was the result of a serious effort to address the concerns and demands of Ristigouche’s citizens,” Judge Nicole Tremblay wrote in her decision. “Public interest, the collective well-being of the community and the safety of residents must be weighed for all projects introduced into a municipality.”

In the absence of any existing provincial laws to protect water sources, the municipality had the right to create its own, the judge added. She ordered the company to cover half of the municipality’s legal fees as well as provide an additional C$10,000 to cover other costs incurred as a result of the lawsuit.

Gastem, which has 30 days to appeal the ruling, did not respond to a request for comment.

As Ristigouche Sud-Est waged the years-long legal battle, support poured in from across Canada. A crowdfunding campaign, launched in 2014 as the township grappled with the idea of legal fees that could reach hundreds of thousands of dollars, has raised more than C$342,000 to date.

Municipal officials estimated that the court battle cost about C$370,000 in legal fees and said any funds remaining would be donated to similar causes.

“Today, we raise our glass of potable water to the health of Quebec’s water and to all of those who supported us,” said Boulay, the mayor. “Thanks to all of you, we were able to defend ourselves – and win.”

Canadian town steams over Nestlé bid to control local spring water well Read more

He cautioned that the battle was far from over. The township has joined forces with more than 350 other municipalities in the province to take aim at a 2014 law that set out a protected perimeter of 500 metres around potable water sources. The municipalities are calling on provincial authorities to expand this protected, no-drill zone to two kilometres.

Jean-François Girard, the lawyer representing the township, described this week’s ruling as a victory, given that the lawsuit was seemingly solely aimed at punishing the municipality for taking a stand. “You have to think about it, the tax base in Ristigouche consists of 84 people,” he told Radio-Canada.

The ruling that emerged could set an important precedent for municipalities as they seek to secure a healthy environment for their residents, he said. “This will force companies that want to sue municipalities to think twice if they don’t have legal grounds.”