U.S. District Judge Lyle Strom signed the order shortly after the walkout began and ordered both sides to appear before him at a hearing Saturday afternoon in Omaha, Neb., where Union Pacific is based.
The Brotherhood of Locomotive Engineers said the strike was the result of new qualification standards for personal leave that were implemented at the start of the year.
"Union Pacific has chosen to unilaterally impose new working conditions on locomotive engineers, in defiance of federal law," Edward Dubroski, president of the Cleveland-based union, said in a statement.
"While we disagree with the court, and are confident that our legal position ultimately will prevail, we will respect the court's order," Dubroski said.
Workers spent three hours picketing before the judge issued the order. The demonstrations likely disrupted freight travel, but it wasn't clear how many trains were affected, said John Bromley, a Union Pacific spokesman.
"It could have been a major problem for the national economy had it been allowed to continue," he said.
Union Pacific has 38,654 miles of track in 23 states. The railroad hauls everything from chemicals, coal and food to grain metals and automobiles. Some employees also operate commuter trains in the Chicago area, and numerous Amtrak trains operate over Union Pacific tracks; those services were not affected.
"Our strike is against the Union Pacific Railroad, not against passenger railroads or the traveling public," Dubroski said.
News of the walkout came as a surprise to the rail carrier, officials said.
"We were completely blindsided," Mike Furtney, regional director of public relations in San Francisco, told the San Antonio Express-News.
Company officers and engineers of other unions could still move trains if the strike were to continue, Furtney said.
The railroad had announced in December that it planned to cut 2,000 jobs by the end of February because of a slowing economy, high fuel prices and harsh winter weather.