WASHINGTON—A federal judge ruled the military must provide immediate access to legal counsel for a U.S. citizen being held as an enemy combatant at a secret location in Iraq.

The unnamed U.S. citizen was captured in September by Syrian opposition forces fighting Islamic State and transferred to U.S. custody in Iraq, court records show. He has yet to be granted access to a lawyer.

Following...