The EU's top court said on Monday that officials can detain migrant rescue ships, but only if they can prove there is a real risk to health, safety or the environment, in mixed guidance on a case testing Europe's response to refugees.

German campaign group Sea Watch launched a legal challenge after Sicilian port authorities detained two of its vessels that had rescued migrants in the Mediterranean and taken them to Sicily in 2020.

Italian judges hearing the complaint asked the European Court of Justice (ECJ) for guidance on the case that goes to the heart of disputes over how to respond to the tens of thousands of people risking the crossing from Africa every year.

Sea Watch, which patrols the Mediterranean to pick up migrants in distress - a practice which some European states say encourages migration - is arguing that the port authorities exceeded their authority by detaining the vessels.

The Sicilian ports of Palermo and Empedocle argued at the time that they had searched and detained the vessels on the grounds that they were overcrowded and not registered for search and rescue operations, according to the ECJ.

The Luxembourg-based EU court delivered a mixed ruling which could support arguments on both sides of the case.

It said port authorities have the right to check and detain ships under certain circumstances, though the mere fact that a vessel is carrying people rescued at the sea does not offer sufficient grounds for that.

938 PEOPLE DEAD THIS YEAR

"The number of persons on board, even if greater than that which is authorised, cannot therefore, in itself, constitute a ground for a control," the court said in a statement.

It also suggested, however, that regularly running search and rescue operations using ships certified for cargo - as was the case for the Sea Watch vessels - could provide sufficient grounds for port authority controls.

Sea Watch welcomed the ruling, saying it provided legal security for NGOs and "a victory for sea rescue".

"The fact that port state controls can continue to take place on NGO ships is a good thing," it said. "They are intended to ensure ship safety, which is important to us. Arbitrary controls, on the other hand, must finally come to an end."

There was no immediate reaction from the Sicilian port authorities.

The ECJ ruling sets out the current state of European law on the issue. But it will be up to the Sicilian court to rule if the specific circumstances in these cases justified the actions by the port authorities.

Around 61,000 people have completed the crossing this year and an estimated 938 have died on the route, according to data from the U.N. refugee agency UNHCR.