Children under five separated from their parents after entering the US illegally must be reunified within 14 days of the order. The court also issued a nationwide injunction on future family separations.
A judge in California has ordered US border authorities to reunite separated families within 30 days.
If the children are younger than five, they must be reunified within 14 days of the order, issued on Tuesday.
US District Judge Dana Sabraw in San Diego issued the order in a lawsuit by the American Civil Liberties Union. The lawsuit involves a seven-year-old girl who was separated from her Congolese mother and a 14-year-old boy who was separated from his Brazilian mother.
Sabraw also issued a nationwide injunction on future family separations, unless the parent is deemed unfit.
More than 2,300 migrant children were separated from their parents after US President Donald Trump's administration began a "zero tolerance" policy in early May, seeking to prosecute all adults who crossed the border illegally, including those travelling with children.
"The facts set forth before the court portray reactive governance responses to address a chaotic circumstance of the government's own making," Sabraw wrote.
"They belie measured and ordered governance, which is central to the concept of due process enshrined in our Constitution."
Sabraw's ruling could force the administration to rapidly address confusion left by Trump's order, and government agencies to scramble to reunite families. The administration can appeal.
The Congolese mother the ACLU had sued on behalf of arrived in the United States to seek asylum and escape religious persecution in Democratic Republic of Congo last November. She was separated then from her six-year-old daughter. While they were reunited in March, the ACLU is pursuing class-action claims on behalf of other immigrants.
Trump issued an executive order to end the family separations on June 20, but the government has yet to reunite about 2,000 children with their parents.
The ACLU said on Monday Trump's order contained "loopholes," and proposed requiring that families be reunited within 30 days, unless the parents were unfit or were housed in adult-only criminal facilities.
On Tuesday, the US government temporarily halted the separation of families as the prosecution of parents necessitated more space for detention which the administration is fast running out of.
Before the preliminary injunction ruling, the US government urged Sabraw not to require that it stop separating and quickly reunite migrant families after they illegally cross the US-Mexico border, saying Trump's executive order last week "largely" addressed those goals.