For Japan, it was because of the importance of the castle. It was built back in the shogun days when the emperor resided in Kyoto and the Shogun in what is now called Tokyo, but was named Edo at the time.
Early castles were often built on hills or by rivers, and some were built on bluffs overlooking rivers. The large ones on flat grounds such as Edo and Osaka were symbols of power rather then defensive.
The Edo castle was built after the long Warring States period and the establishment of the Tokugawa shogunate, and the daimyo (feudal lords) were required to contribute.