A Brooklyn federal judge took on President Trump Saturday night, staying deportations that could have immediately sent people from seven predominately Muslim countries back to where they came from.

An estimated 100 to 200 people had been detained at airports around the country when their planes landed Friday and Saturday.

Judge Ann Donnelly’s order applies to all of them, not only to those arriving within her immediate jurisdiction at JFK Airport.

An emergency hearing on the matter ended dramatically when ACLU lawyer Lee Gelernt told Donnelly that at least one person at JFK was being put on a flight back to Syria at that moment.

The judge asked if the Trump administration could assure that the people about to be deported would not suffer irreparable harm.

Susan Riley, a civil lawyer who works in the Brooklyn US Attorney’s Office, said she couldn’t answer the question.

“This has unfolded with such speed that we haven’t had any opportunity to address any of the issues, the legal issues of the status of anyone who may be at the airport,” Riley said.

Donnelly then asked, “If they had come in two days ago we wouldn’t be here. Am I right?”

The judge noted that the government did not argue that the detainees posed any risk.

Gelernt said the feds had not given up the names of all the people who were detained around the country.

Still, he insisted the detainees posed no risk.

“It’s not as if these people weren’t vetted, they were just caught in transit,’’ he said. “They were in a horrible position.”

It was not clear how the feds would respond to the decision by Donnelly, who was appointed to the federal bench by former President Barack Obama.

Donnelly first made news as a former Manhattan assistant district attorney in such high-profile cases as the successful prosecution of thieving former Tyco International Dennis Kozlowski.

“Ann Donnelly was a smart, tough, fair prosecutor,” said defense lawyer and former prosecutor’s office colleague Daniel Bibb.

“She had that same reputation as a state Supreme Court justice, and that reputation earned her her place on the district court bench,” he said.

The refugees will not be deported immediately, but may find themselves in government detention for several weeks at least.

Further proceedings are set for next month.

A crowd of at least 1,000 gathered outside the courthouse erupted in cheers when Donnelly’s ruling was announced.

“I’m thrilled — it just feels like every day there is another horrible thing; it’s constant,” said protester Jennifer Palumbo, 34, of Brooklyn Heights.

Trump, she said, “is impulsive. I want to say he doesn’t think about the consequences of what he does as president, but I honestly think he just doesn’t care.”

Protesters cheered as the ACLU lawyers left.

Two of the JFK detainees — both of whom had links to US forces in Iraq and were in danger of being killed if they were sent back — had already been sprung before the emergency hearing.

They were Hameed Khalid Darweesh and Haider Sameer Abdulkhaleq Alshawi.

When their lawyers asked an airport customs supervisor for help, they were told, “Mr. President. Call Mr. Trump,’’ they complained in court papers.

The detentions had been part of Trump’s effort to keep refugees from Syria, Iran, Iraq, Libya, Somalia, Sudan and Yemen out of the country.

Going forward, travelers from those seven countries likely willstill be barred from boarding US-bound flights.

Trump’s ban stays in effect for 120 days.

In Philadelphia, two Syrian families living in Qatar were detained when they arrived and then sent back, relatives told NBC News.

More than a dozen were detained in Chicago.

Fifty people were detained at the Dallas-Forth Worth Airport.

By midafternoon, nine remained. It’s unclear what happened to the 41 others.

Among those detained in Dallas were a Syrian couple visiting their sons, one of whom attends Southern Methodist University.

Trump’s order sowed confusion around the world as authorities rushed to comply.

The ACLU’s Jadwat said that when Trump signed the order, “there was no actual plan in place for how to implement it.”

The president strongly disagreed.

“It’s not a Muslim ban, but we’re totally prepared,” Trump said.

“It’s working out very nicely. You see it at the airports, you see it all over. It’s working very nicely.”

Another JFK detainee may be Stony Brook University student Vahideh Rasekhi, according to friends who posted on social media.

The linguistics graduate student had recently made her first trip home to Iran in five years and tried to re-enter the country Friday.

“She’s been living here probably more than the total amount of time she’s been living in Iran,” said her friend, Aida Nikou.

“Her not being able to come back is just so bizarre.”

Relatives of detainees, desperate for information, gathered throughout the day at JFK.

“I haven’t seen her for seven years, and now they want to send her back,” said Elaf Hussain, of her mom, Iman, who had arrived from Iraq after a 17-hour trip.

“I’m really scared right now. She has been trying for two years to get her visa, and she just got it on Jan. 16.

“She has been vetted,” Hussain told The Post as she waited for word outside a JFK terminal.

“I don’t know what the problem is.”

Reps. Jerrold Nadler (D-Manhattan) and Nydia Velazquez (D-Brooklyn) joined the protest at the airport.

“This should not happen in America. We shouldn’t have to demand the release of refugees one by one,” Nadler and Velazquez said in a joint statement.

Government officials for weeks had planned the implementation of the order, one source said.

Additional reporting by Daniel Halper, Nick Fugallo and Leonica Valentine