In a 5-2 decision overturning two lower court rulings, the Supreme Court said bankruptcy was not "a license to ignore rules," and trustees couldn't simply walk away from the liabilities of their estate.
The ruling came in a case involving Redwater Energy, which became insolvent in 2015, owing Can$5.1 million ($3.9 million) to the bank ATB Financial.
The bankruptcy trustee had hoped to sell off 17 of its producing oil and natural gas wells to pay off creditors while abandoning dozens of inactive wells.
But the Alberta energy regulator and the Orphan Well Association -- a non-profit that decommissions abandoned oil and gas wells -- demanded that the company meet its environmental cleanup obligations.
The ruling backs a licensing condition that energy companies plug and cap wells at the end of their life, as well as dismantling them and cleaning up contaminants on the land -- known as reclamation.
The case was watched closely by oil and gas producing provinces worried about cleanup costs falling on them, such as Saskatchewan and British Columbia, as well as the energy industry and environmental activists.
The ruling "has rightfully put the health of our environment ahead of banks," said Joshua Buck, of pressure group Environmental Defense.
The Orphan Well Association lists more than 3,000 abandoned wells that need to be plugged and a further 1,500 sites that need to be reclaimed in Alberta.
A recent investigation by the Global and Mail newspaper pointed to a looming financial and environmental crisis, with 20 percent of the 610,696 oil and gas wells in Alberta, British Columbia and Saskatchewan inactive.
Almost two-thirds of these 122,456 idle wells have been shut off for five years or more and with few prospects of them ever being reopened.
A growing number of the wells are owned by companies that can least afford to clean them up, said the newspaper, and cleanup costs have overwhelmed dedicated cleanup funds set up by the industry.