The Federal Administrative Court in Leipzig said the cities of Stuttgart and Duesseldorf could legally ban older, more polluting diesel cars from zones worst affected by pollution.
The ruling sets a precedent for other cities and analysts said it could lead to similar action across Europe.
The government, which had opposed the bans, said they could still be avoided.
The ruling by a top federal court came after German states had appealed against bans imposed by local courts in Stuttgart and Duesseldorf.
The environmental group DUH brought the cases after about 70 German cities exceeded European Union limits for nitrogen oxides (NOx) last year.
Diesel emissions containing nitrogen oxides can cause respiratory disease.
Read more: BBC