Non-EU migrants illegally entering an EU state in the Schengen zone should not face detention on those grounds, says the European Court of Justice.
Migrants staying illegally should instead be returned to the country from which they came under the so-called Return Directive, it said.
The ruling applies to migrants crossing borders within the passport-free area and on leaving the zone.
It will infuriate critics of EU policy, but contains several important caveats.
The ruling was triggered by the case of a Ghanaian migrant who was found to be using false Belgian travel documents by French police at the entrance to the Channel Tunnel.
Selina Affum was placed in police custody on grounds of illegal entry into French territory but argued that this was unlawful, in light of the EU's Return Directive.
Under the directive, an illegal migrant told to leave has up to 30 days to go voluntarily. After that, removal should not involve excessive force or place the person's life in danger.
The French court of cassation referred Ms Affum's case to the European Court of Justice, the EU's highest court.
"The Return Directive prevents a national of a non-EU country who has not yet been subject to the return procedure being imprisoned solely because he or she has entered the territory of a Member State illegally across an internal border of the Schengen area," the Luxembourg-based court ruled.
This was also the case when the migrant "is intercepted when leaving the Schengen area".
The ruling does not apply to the UK or Ireland, which are not within the Schengen zone. It also does not apply to Denmark, which although a member of the Schengen zone holds an opt out from European Union justice policies.
The ruling also contains some important conditions.
It says migrants may be detained - for up to 18 months - if there is "a risk of the removal being compromised", which may be interpreted to mean that they are a flight risk.
Detention is also permitted if a person is subject to a deportation order and has refused to go, or if they have already been deported, and have attempted to re-enter the country illegally, in breach of an entry ban.