According to an amazing website on international driving:
Although the origin of this habit goes back to the Edo period (1603-1867) when Samurai ruled the country, it wasn’t until 1872 that this unwritten rule became more or less official. That was the year when Japan’s first railway was introduced, built with technical aid from the British. Gradually, a massive network of railways and tram tracks was built, and of course all trains and trams drove on the left-hand side. Still, it took another half century till in 1924 left-side driving was clearly written in a law.The website also lists why and how countries have switched from driving on the left to driving on the right (most of the countries that remain on the left are island, and so don't have to worry about driving into right driving countries). The best anecdote is:
Pakistan also considered changing to the right in the 1960s, but ultimately decided not to do it. The main argument against the shift was that camel trains often drove through the night while their drivers were dozing. The difficulty in teaching old camels new tricks was decisive in forcing Pakistan to reject the change.Amazing.