A judge has released an undocumented Ecuadorean immigrant who was detained by immigration officials after delivering pizza to a military base.

US District Judge Paul Crotty ordered Pablo Villavicencio, 35, to be freed on Tuesday from immigration custody, where he has remained for 53 days.

Mr Villavicencio was arrested on 1 June at Brooklyn's Fort Hamilton military base after a routine background check.

The father-of-two is married to a US citizen and is awaiting a visa.

Judge Crotty ruled that Immigrations and Customs Enforcement (ICE) must release Mr Villavicencio as his "removal is no longer reasonably foreseeable" and temporarily postponed his deportation.

He wrote that despite remaining in the US illegally after agreeing to leave the country in 2010, Mr Villavicencio had a right to exhaust his options to legally remain in the US before being removed.

"He now has two children, both of whom are United States citizens," Judge Crotty wrote. "He has no criminal history. He has paid his taxes. And he has worked diligently to provide for his family."

Since marrying his wife, Sandra Chica, who is a US citizen, Mr Villavicencio began seeking permanent residency. He has been living in the US for nearly 10 years.

According to the ruling, the government is currently processing his petition and has scheduled an interview for him.

Mr Villavicencio's wife and two children were present at Tuesday's hearing, but he remained at the New Jersey detention facility where he has been held since his arrest.

At the hearing, US media reported that Judge Crotty asked immigration authorities: "Is there any sense of justice here?"

"Are we just doing this because we want to? Why do we want to enforce the order? It makes no difference in terms of the larger issues facing the country."

After the announcement of Mr Villavicencio's release, both New York Governor Andrew Cuomo and New York City Mayor Bill de Blasio tweeted support, but criticised the fact that he was arrested at all.

Legal Aid Society attorney Adriene Holder, the firm representing Mr Villavicencio, said "the rule of law, humanity and morality prevailed".

"This decision should serve as a rebuke against the Trump Administration and its merciless crusade to tear families apart," Ms Holder said in a statement published on the Society's website.

"The Villavicencio family has finally received a crucial measure of relief from their 53-day nightmare and we will continue to fight alongside them to protect their right to remain in the community they call home."

Mr Villavicencio was asked for government ID when delivering pizza to Fort Hamilton military base. When he could not produce one, base officials said, he consented to a background check.

The background check revealed he had an outstanding order to leave the country and was considered a fugitive. He was handed over to federal immigration officials.

His arrest sparked outrage in New York, and protesters have been demonstrating against ICE and calling for his release since the case began.

A federal judge had previously blocked his deportation on 11 June after immigration officials had requested an expedited removal.