In the 19th century, Japan was far from the economic and political power we know nowadays. The Tokugawa system, feudal in most aspects, was based on the strict class hierarchy originally established by Toyotomi Hideyoshi. The warrior-caste of samurai were at the top, followed by farmers, artisans, and traders.
The samurai, traditionally a warrior class that was at the service of a lord, had increased their political and social influence during the Tokugawa period, by becoming landowners, courtiers, bureaucrats, and administrators rather than warriors. However their aristocratic status still kept the same warrior meaning influenced by the teachings of Confucius and Mencius. They were an educated class, had a complex code of obligations towards their lord (usually a daimyo), and a formalized chivalry code (called "Bushido"). They had the legal right to wear weapons (they actually were the only granted that privilege) and to cut down any commoner who did not show proper respect, although to what extent this right was used is unknown.
As a consequence of the rigid structure that composed the social system, Japan was a backward country when compared to the overwhelming power of the European and American empires of the time. Like other subjugated Asian nations, the Japanese were forced to sign unequal treaties with Western powers, which granted them one-sided economical and legal advantages in Japan. The very inflexibility of the caste system, lacking all forms of economic dynamism and neglecting inflation effects, unleashed huge economic crisis and general empoverishment.
The Meiji Restoration
En 1867, an alliance of several of the most powerful daimyo (Satsuma and Kido) with the titular Emperor finally succeeded in the overthrow of the Tokugawa shogunate after the Boshin War, culminating in the Meiji Restoration. The power of the Emperor became increasingly stronger, and after a series of confrontations against his former allies, he gained all the lands under his own control. The result of this deep change in the political system was the establishment of a centralised power supported by a completely new oligarchy that undertook a series of reforms in the country.
The Emperor was convinced that Japan needed to become a World power by following the model of the Western empires. This way, the four-caste division of the society was abolished and freedom of social and occupational mobility was guaranteed. The education became compulsory, and the education system reformed after the French and German ones. Freedom of worship was also established, a constitution was edited and several forms of democratic participation of the citizens founded. Agrarian reforms and massive industrial development were succesfully undertaken, the concept of a market economy and the adoption of British and North American forms of free enterprise capitalism were welcomed by a country that soon showed its abundance of aggressive entrepreneurs. As a consequence, Japan emerged as the first Asian industrialised nation and one of the new great World powers.
What happened to the samurai?
All this logically meant the end for the samurai. As they received fixed revenues from the government, their upkeep was a tremendous financial limit for a nation in development (there were 1.9 million samurais at the time), so the Meiji government started a slow process to progressively abolish this social class. Most of their salaries were diminished or cancelled, and the ones that remained had to convert them into government bonds (which, of course, had no guarantee of being valid if the central government failed). A Western-like nobility classification was established among the remaining samurai and courtiers: prince, marquis, count, viscount, and baron were new conceded entitlements.
The reform of the military was the real end of the samurai privileges. In 1873, nation-wide conscription was established, and the right to bear arms was extended to every male in the nation. In this manner, the samurai lost their right to be the only armed force in favor of a modern, Western-like army. Moreover, their honorific right to wear a katana in public was eventually abolished along with the right to cut down commoners who paid them disrespect.
Although many samurai became wandering warriors and some riots arose (such as the major Satsuma rebellion, lead by the samurai Saigo Takamori), most samurai adapted easily to their new condition. Many found employment as policemen or in the government bureaucracy, which resembled an elite class in its own right. The samurai, being better educated than most of the population, became teachers, government officials or military officers. However, the ideal of samurai military spirit lived on in romanticised form and was often used as nationalistic propaganda during the 20th century.