A federal judge late Saturday blocked part of President Donald Trump’s executive order barring entry of any non-U.S. citizens from seven majority-Muslim countries, ruling that people already in airports across the United States should not be sent back — a partial victory for immigration attorneys who had challenged the detentions nationwide.

U.S. District Court Judge Ann M. Donnelly issued the order after an emergency hearing in federal court in Brooklyn, where lawyers from the American Civil Liberties Union and the National Immigration Law Center, among others, had sought a complete stop to Trump’s order.

The news brought cheers from hundreds of people gathered outside the courthouse, and hundreds more protesters who had demonstrated throughout the day at Kennedy Airport’s international arrivals terminal.

Donnelly’s order also cemented the freedom of two Iraqi migrants who had been detained at Kennedy.

The hastily called hearing stemmed from a lawsuit on behalf of Hameed Khalid Darweesh and Haider Sameer Abdulkhaleq Alshawi. In addition to the two men’s release, the lawsuit had sought an injunction barring the detention of any migrant detained because of Trump’s executive order and the release of any already detained.

Immigration lawyers at the airport had said earlier that at least 11 people still were being detained there after traveling from Muslim-majority countries — part of such detentions nationwide, said Becca Heller, director of the International Refugee Assistance Project.

“There are hundreds of refugees stranded in this country,” she said. “They live in danger and are trying to reunite with their families and as soon as they landed, they were put in handcuffs.”

Heller said she believed there were dozens more who may have arrived at Kennedy or, at that point, still were on aircraft in flight. She called on U.S. Customs and Border Protection to release the names of those detained so they could connect with families and attorneys.

Gov. Andrew M. Cuomo, in a statement Saturday evening, said he had directed the Port Authority, the New York Department of State and his counsel’s office “to assist anyone detained at New York airports, and ensure that their rights are protected. America is a nation of laws, and those laws provide rights that must be respected and followed regardless of political ideology.”

Trump, in signing the executive order Friday afternoon, said he was taking the action to help protect Americans from terrorist attacks. On Saturday, he said the crackdown on citizens from Iraq, Iran, Libya, Somalia, Sudan, Syria and Yemen and refugees “is not a Muslim ban.”

The executive order suspends the entry of all refugees for 120 days, suspends indefinitely the entry of Syrian refugees, and bars the entry of citizens of those seven countries for 90 days.

House Speaker Paul Ryan was supportive of the executive action, saying in a statement Friday that the visa-vetting process must be strengthened.

“Our number-one responsibility is to protect the homeland,” Ryan said. “President Trump is right to make sure we are doing everything possible to know exactly who is entering our country.”

The effects of the executive order reverberated around the globe, with people in the midst of seeking green cards, those with student visas and those with temporary work visas feeling uneasy about what it might mean for them and their families.

Among others blocked from entering the country, for example, was Iranian scientist Samira Asgari, who was headed to Boston, but, according to a tweet from her, was “denied boarding due to my Iranian nationality. Feeling safer?” Asgari was living in Switzerland, her Twitter account said.

Iran had quickly retaliated against the Trump-imposed ban against its citizens with its own action, on Saturday saying it is blocking U.S. citizens from entering Iran.

“While respecting the American people and distinguishing between them and the hostile policies of the U.S. government, Iran will implement the principle of reciprocity until the offensive U.S. limitations against Iranian nationals are lifted,” a Foreign Ministry statement said.

“The restrictions against travel by Muslims to America . . . are an open affront against the Muslim world and the Iranian nation in particular and will be known as a great gift to extremists,” said the statement, carried by state media.

Meanwhile, at Kennedy, hundreds of protesters — expressing vehement opposition to Trump’s executive orders — gathered outside Terminal 4 for international arrivals, growing from about a dozen people at midday to an estimated 2,000 by the afternoon. They remained into the evening, walking in the roadways while chanting.

People arriving at the terminal threaded their way through the crush, as officers with the Port Authority Police and NYPD stood by. Mayor Bill de Blasio said he sent his commissioner of immigrant affairs to deliver coffee to the demonstrators, and filmmaker Michael Moore was among the crowd, using social media to rally support. As darkness fell, there had been no reports of arrests.

Reps. Jerry Nadler and Nydia Velazquez, both New York City Democrats, and lawyers with the International Refugee Assistance Project negotiated the release of Darweesh, who had worked for the U.S. military in Iraq as an interpreter.

The lawsuit on behalf of Darweesh and Alshawi says the men’s detention violates their constitutional due process rights and is outside immigration law. Both men had cleared security checks, the lawsuit says.

Darweesh, after arriving Friday on a flight from Iraq, was first interrogated for hours before he reached an attorney and his family, Heller said.

Outside Terminal 4, he was was reunited with his wife and children, who had traveled on the same flight and were released Friday night.

Darweesh had been granted a “special immigrant visa” last week, the lawsuit said, because he worked for the U.S. Army as an interpreter, for a federal agency as an engineer and for a federal contractor.

“The soul of America pushes me to leave my country and come here,” Darweesh said outside the terminal. “America is the land of freedom and the right. I’m very thankful . . . I have a special visa in my passport. I support the U.S. government, but they said they have a policy, and said I broke the rules.”

Darweesh said he handed over his passport, and customs officials put it in an envelope without explanation.

He left the airport terminal flanked by Nadler, Velazquez and immigration attorneys and was greeted by protesters chanting, “No ban! No wall! Donald Trump has got to fall!”

“They told me, ‘Don’t worry, this is America. There is a constitution, and I will be freed,’ ” Darweesh said. “When I get out, I was surprised.”

Alshawi was granted a visa earlier this month to join his wife and son in the United States, the suit says. They had been given refugee status because of the family’s association with the U.S. military.

Heller said Alshawi was allowed to call his wife but had yet to speak to an attorney. “His wife was persecuted in Iraq for his work helping the U.S. and she was desperate to reach him,” she said.

Velazquez, speaking before television cameras and reporters outside the terminal, directed her comments to Trump.

“I am begging you revisit this,” she said. “It is ill-advised and tearing families apart . . . It will undermine the cooperation we need from Muslim countries to fight terrorism.”

Nadler said Darweesh was “a marked man for his work with American troops, not a potential terrorist.”

“This executive order is discriminatory on religion and quite disgusting,” Nadler said. “Not a single terror incident since 9/11 has been caused by any member of a country on the banned list.”

Nadler and Velazquez said those given valid visas should be permitted to enter the country.

“Mr. President, look at us. This is America,” an impassioned Velazquez said. “What you have done is shameful.”

Attorneys said they were negotiating the release of at least one more person at Kennedy.

At the protest, Ladslav Leitner, 45, of the Bronx, held a sign that said “Impeach Trump, deport Melania,” who was born in Slovenia.

“I’m here to show how ridiculous the ideas from the White House are,” Leitner said. “It’s insane. He’s hurting immigrants and hurting asylum-seekers.”

Murad Awawdeh, political director of the Immigrant Action Fund, said protests won’t stop until all the detainees are released.

“This is not right. This is beyond disgusting,” Awawadeh said. “This is not who are as Americans.”

Also joining in the lawsuit against the president and federal agencies were the Jerome N. Frank Legal Services Organization at Yale University Law School and the Manhattan law firm of Kilpatrick Townsend and Stockton.

With Matthew Chayes and Reuters