BALTIMORE (AP) — A federal judge in Maryland has banned immigration officials from arresting, detaining and deporting immigrants who are seeking legal status based on their marriages to U.S. citizens.

U.S. District Judge George J. Hazel issued the ruling Friday in a case filed by six couples accusing immigration officials of luring families to marriage interviews in Baltimore, only to detain the immigrant spouse for deportation. Hazel also ordered U.S. Immigration and Customs Enforcement to release from custody immigrants married to U.S. citizens detained before they could complete the first step of the process to obtain legal residency.

Federal regulations allow U.S. citizens to try to legalize the status of spouses who have been living in the country illegally, including those with deportation orders. But the American Civil Liberties Union, which is representing the Maryland couples, has argued that a growing number of officers have “cruelly twisted” the rules by detaining immigrant spouses following the required marriage interviews.

The months-long process typically requires couples to demonstrate the legitimacy of their marriage as part of the first step. If the couples pass an interview and earn other approvals, immigrant spouses eventually must travel abroad for a visa interview at a U.S. consulate. Only if they receive a visa can they return to the U.S. legally.

It’s unclear how many people have become permanent U.S. residents through the Obama-era regulations. They were created to significantly reduce the time that U.S. citizens are separated from immediate family members while trying to build a lawful immigration case from their home countries.

WATCH: For-profit prisons reap business benefit from Trump’s immigration stance

“(T)his order is in the public interest because it requires Respondents to comport with their own rules and regulations, bars arbitrary agency action toward vulnerable immigrant communities, and diminishes the emotional and financial impact on families participating in the provisional waiver process,” Hazel wrote. He has not ruled on the class certification sought by the plaintiffs.

ICE did not immediately respond to a request for comment. The government in its response filed last year asked Hazel to dismiss the case arguing the court lacked jurisdiction and the plaintiffs’ “claims are not likely to be successful.”

The ruling only applies to anyone living in Maryland, but the ACLU is also pursuing a similar complaint in Massachusetts. It is unclear how many people must be released from custody under Hazel’s ruling.

Among the plaintiffs is Elmer Sanchez, a native of Honduras who was ordered in absentia to be deported in 2005 and married a U.S. citizen in 2013. He was taken into custody in May moments after he and his wife successfully finished their interview with an immigration officer in Baltimore. He was released six weeks later after the ACLU sought an emergency order to prevent imminent deportation.

“These families endured trauma under the actions of ICE,” ACLU of Maryland attorney Nick Steiner said in a statement Monday. “Their blatant disregard for their own rules and regulations that are meant to prevent family separation is unconstitutional. We are hopeful about the ruling and we believe it’s a step in the right direction in recognizing that immigrants have constitutional protections too.”