ATHENS—A Greek court ruled that migrants arriving on Aegean islands near Turkey should be allowed to travel freely around the country, officials said, a decision that could alleviate dire conditions on the islands, but might derail a pact between the European Union and Turkey to keep a lid on arrivals.

Greece’s highest administrative court ruled Wednesday that there are no “serious or overriding reasons of public interest” to justify the government’s prohibition on migrants’ leaving the islands and traveling to the Greek mainland,...