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.”

But Mr. Morrison’s government says the detention policy has saved lives by discouraging asylum seekers from trying to make the dangerous sea voyage from Indonesia. Before the policy was introduced in 2013, overcrowded boats often capsized while trying to reach Australia.

“This Parliament has already tipped its hand enough to the people smugglers,” Mr. Morrison said in Canberra, the capital, before the vote on Wednesday. “To anyone who thinks they should get on a boat: I’m here and I will stop you.”

Mr. Morrison, a former immigration minister, said he would reopen a detention center on Christmas Island, an Australian island territory, to address the prospect of future arrivals. He said he would also press for tougher national security steps in response.

His government had argued that the bill was a step toward ending the detention policy, and that some of the asylum seekers would be a security threat if allowed into the country. The new law includes a number of amendments that were added to assuage such concerns.

It says that asylum seekers and their families can be brought to Australia for medical treatment if two or more doctors say it is needed. But the home affairs minister can reject the request. An independent medical panel would review that decision, but the minister can still refuse the medical transfer on national security grounds, or if the asylum seeker has a serious criminal record.

Mr. Morrison said the vote showed that the Labor Party could not be trusted with Australia’s security. The Labor Party backed the medical transfer legislation but supports the broader offshore detention policy.

Behrouz Boochani, a Kurdish-Iranian writer who has been on Manus Island for more than five years, welcomed the vote on Twitter.

“Many people are happy now because they will finally receive medical treatment,” Mr. Boochani said. “Great to see the Australian parliament finally vote for humanity.”