The Japanese daimyo Akamatsu Sadanori built the first iteration of Himeji Castle (originally called "Himeyama Castle") in 1346, just west of the city of Kobe. At that time, Japan was suffering from civil strife, as happened so often during feudal Japanese history. This was the era of the Northern and Southern Courts, or Nanboku-cho, and the Akamatsu family needed a strong fortress for protection against neighboring daimyo.
Despite the moats, walls and high tower of Himeji Castle, the Akamatsu daimyo was defeated during the 1441 Kakitsu Incident (in which the shogun Yoshimori was assassinated), and the Yamana clan took control of the castle. However, the Akamatsu clan was able to reclaim their home during the Onin War (1467-1477) which touched off the Sengoku era or "Warring States Period."
In 1580, one of Japan's "Great Unifiers," Toyotomi Hideyoshi, assumed control of Himeji Castle (which had been damaged in the fighting) and had it repaired. The castle passed to the daimyo Ikeda Terumasa after the Battle of Sekigahara, courtesy of Tokugawa Ieyasu, founder of the Tokugawa dynasty that ruled Japan until 1868.
Terumasa again rebuilt and expanded the castle, which had been almost completely destroyed. He completed renovations in 1618.
A succession of noble families held Himeji Castle after the Terumasas, including the Honda, Okudaira, Matsudaira, Sakakibara, and Sakai clans. The Sakai controlled Himeji in 1868, when the Meiji Restoration returned political power to the Emperor, and broke the samurai class for good. Himeji was one of the shogunate forces' last strongholds against the imperial troops; ironically, the Emperor sent a descendant of restorer Ikeda Terumasa to shell the castle in the final days of the war.
In 1871, Himeji Castle was auctioned off for 23 yen. Its grounds were bombed and burned during World War II, but miraculously the castle itself was almost entirely undamaged by the bombing and fires.