SYDNEY, Australia — The Australian Parliament on Wednesday narrowly passed legislation that would let some asylum seekers being held on remote Pacific islands come to Australia for medical treatment, a blow to Prime Minister Scott Morrison, who had strongly opposed the measure.

Under Australia’s offshore detention policy, asylum seekers who try to reach the country by boat are barred from ever settling there. About 1,000 migrants intercepted at sea have spent years on the tiny island nation of Nauru or on Manus Island in Papua New Guinea, under conditions that visiting experts have described as dire.

The detention policy has been condemned by the United Nations and various human rights groups, and Australians who oppose it celebrated the 75 to 74 vote in the House of Representatives on Tuesday and the 36 to 34 vote in the Senate on Wednesday.

“This victory breaks the political deadlock that has poisoned our refugee policy for years,” Hugh de Kretser, executive director of the Human Rights Law Center, said on Twitter. “It sends a message that the cruelty must stop.”