The collapse of the Fundão dam in the south-east of the country caused a giant mudslide that killed 19 people.
It also severely polluted the Rio Doce River, compromising the waterway to its outlet in the Atlantic Ocean.
It was not immediately clear how much each company is required to pay.
Judge Vinicius Cobucci said the money would be put in a state fund and used for projects and initiatives in the area affected by the dam collapse.
The ruling came in response to a civil action brought by state and federal public prosecutors.
Vale told the BBC that it had not been informed about the ruling.
The company also said that as of December last year, the Renova Foundation, which the firms have been using to make compensation payments, has so far paid out 34.7bn reais.
BHP and Samarco did not immediately respond to requests for comment from the BBC.
The ruling allows for the companies to appeal the decision.
Samarco is a 50-50 joint venture between Australian mining group BHP and Brazil's Vale.
The disaster in Minas Gerais state, which also displaced 700 people, is considered one of the country's worst environmental disasters.
When the dam burst, it unleashed a deluge of thick, red toxic mud that wiped out the village of Bento Rodrigues.
It also polluted the Rio Doce river and Atlantic Ocean 650km away, devastating wildlife and tainting drinking water for hundreds of thousands of people.
A report released in 2016 found that the collapse of the dam was due to design flaws.
The technical report, commissioned by Samarco's joint owners BHP and Vale, did not assign blame for the disaster.
Dams holding mining waste, known as "tailings", commonly have walls made of a mixture of sand-like particles and clay-like silt.
The report said that a change in the Fundão dam's design between 2011 and 2012 led to less efficient water drainage, and ultimately to the dam's collapse on 5 November 2015.
Sand in the dam walls became saturated, and abruptly started to behave more like a liquid, in a process known as "liquefaction".
A small earthquake on the day of the dam burst may also have "accelerated" the failure, the report added.
The disaster triggered intense scrutiny of safety policies in the mining industry.
BHP and Vale also face a class action lawsuit in the UK with more than 700,000 claimants.
In January 2019, another tailings dam owned by Vale collapsed in the same state near the town of Brumadinho, resulting in 270 deaths.
By Peter Hoskins
Business reporter