U.S. District Judge William Quarles dismissed the newspaper's lawsuit, saying the paper wrongly asserted a greater right to access to government officials than private citizens have.
"The right to publish news is expansive. However, the right does not carry with it the unrestrained right to gather information," the judge ruled.
Sun editor Tim Franklin called the ruling "scary" and said the newspaper would seek an expedited hearing from the 4th Circuit Court of Appeals.
"Essentially, what the court is saying is that it's OK for a politician to create an enemies list," Franklin said. "It's not only unconstitutional, it's undemocratic."
Ehrlich told reporters about the victory at an event in suburban Washington but declined further comment, saying he needed to review the decision first.
The ruling was the latest step in a bitter dispute between Maryland's first Republican governor in 34 years and the state's biggest newspaper.
Ehrlich last fall barred state employees from talking to The Sun's State House bureau chief David Nitkin and columnist Michael Olesker. The governor said the two journalists were not objectively covering his administration. Ehrlich views the two metropolitan daily newspapers serving Maryland, The Sun and The Washington Post, as liberal and pro-Democratic.
The Sun sued in December, saying the order violated the journalists' First Amendment rights. The state asked that the case be thrown out.
Journalism organizations in Maryland and across the country expressed fears Monday that Ehrlich's ban could have a chilling effect on the media.
"The governor's order was really unprecedented in its scope. It took the normal adversarial relationship between the press and government and elevated them to a state of war," said Washington attorney Kevin Baine, whose firm filed a friend-of-the-court brief on behalf of several media organizations including The Associated Press.
By Wiley Hall