The Federal Administrative Court in Leipzig said the cities of Stuttgart and Duesseldorf could legally ban more older, more polluting diesel cars from zones worst affected by pollution.
The government had opposed the bans, which set a precedent for the country, arguing they would cause disruption.
Analysts said the decision could lead to similar action across Europe.
The ruling by the country's highest federal administrative court came after German states had appealed against bans imposed by local courts in Stuttgart and Duesseldorf, in cases brought by environmental group DUH.
The group said bans were necessary after about 70 German cities exceeded European Union nitrogen oxide (NOx) levels limits last year.
Diesel emissions containing nitrogen oxide can cause respiratory disease.