However with the defeat of Napoleon's France in 1814, Bavaria was compensated for some of its losses, and received new territories such as the Grand Duchy of Würzburg, the Archbishopric of Mainz (Aschaffenburg), parts of the Grand Duchy of Hesse, and in 1816, the Rhenish Palatinate from France.
On 26 May 1818, the constitution of the Kingdom of Bavaria was proclaimed. The parliament would have two houses, an upper house comprising the aristocracy and noblemen, including the high-class hereditary landowners, government officials and nominees of the crown. The second house, a lower house, would include representatives of small landowners, the towns and the peasants. The rights of Protestants were safeguarded in the constitution with articles supporting the equality of all religions, despite opposition by supporters of the Roman Catholic Church. The initial constitution almost proved disastrous for the monarchy, with controversies such as the army having to swear allegiance to the new constitution. The monarchy appealed to the Kingdom of Prussia and the Austrian Empire for advice, the two refused to take action on Bavaria's behalf, but the debacles lessened and the state stabilized with the accession of Ludwig I to the throne following the death of Maximilian in 1825.
In 1825, Ludwig I accended to the throne of Bavaria. Under Ludwig, the arts flourished in Bavaria, and Ludwig personally ordered and financially assisted the creation of many neoclassical buildings and architecture across Bavaria. Ludwig also increased Bavaria's pace towards industrialization under his reign. In foreign affairs under Ludwig's rule, Bavaria supported the Greeks during the Greek War of Independence with his second son, Otto being elected King of Greece in 1832. As for politics, initial reforms advocated by Ludwig were both liberal and reform-oriented. However, after the Revolutions of 1830, Ludwig turned to conservative reaction. In 1837, the Roman Catholic-supported clerical movement, the Ultramontanes, came to power in the Bavarian parliament and began a campaign of reform to the constitution, which removed civil rights that had earlier been granted to Protestants, as well as enforcing censorship and forbidding the free discussion of internal politics. This regime was short-lived due to the demand by the Ultramontanes of the naturalization of Ludwig I's Irish mistress, which was resented by Ludwig, and the Ultramontanes were pushed out.
Following the Revolutions of 1848 and Ludwig's low popularity, Ludwig I abdicated the throne to avoid a potential coup, and allowed his son, Maximilian II, to become the King of Bavaria. Maximilian II responded to the demands of the people for a united German state by attending the Frankfurt Assembly, which intended to create such a state. Maximilian II stood alongside Bavaria's ally, the Austrian Empire, in opposition to Austria's enemy, the Kingdom of Prussia, which was to receive the imperial crown of a united Germany. This opposition was resented by many Bavarian citizens, who wanted a united Germany, but in the end Prussia declined accepting the crown and the constitution of a German state they perceived to be too liberal and not in Prussia's interests.
With France's defeat and humiliation against the combined German forces, it was Ludwig II who proposed that Prussian King Wilhelm I be proclaimed German Emperor or "Kaiser" of the German Empire ("Deutsches Reich"), which occurred in 1871 in German occupied Versailles, France. The territories of the German Empire were declared, which included the states of the North German Confederation and all of the south German states, with the major exception of Austria. The Empire also included the formerly French territory of Alsace-Lorraine, which was annexed in large part due to Ludwig's desire to move the French frontier away from the Palatinate.
Bavaria's entry into the German Empire changed, from jubilation over France's defeat, to dismay shortly afterward, over the direction of Germany under the new German Chancellor and Prussian Prime Minister, Otto von Bismarck. The persecution of the Catholic Church in Bismarck's Kulturkampf frustrated the predominantly Catholic southern German states, including Bavaria, although Bismarck was eventually compelled to moderate his policies.
After Bavaria's unification into Germany, Ludwig II became increasingly detached from Bavaria's political affairs and spent vast amounts of money on personal projects, such as the construction of a number of fairytale-like castles and palaces, the most famous being the Wagnerian-style Castle Neuschwanstein. Although Ludwig used his personal wealth to finance these projects instead of state funds, the construction projects landed him deeply in debt. These debts caused much concern among Bavaria's political elite, who sought to persuade Ludwig to cease his building; he refused, and relations between the government's ministers and the crown deteriorated.
At last, in 1886, the crisis came to a head: the Bavarian ministers deposed the king, organizing a medical commission to declare him insane, and therefore incapable of executing his governmental powers. A day after Ludwig's deposition, the king died mysteriously after asking the commission's chief psychiatrist to go on a walk with him along Lake Starnberg (then called Lake Würm). Ludwig and the psychiatrist were found dead, floating in the lake. An autopsy listed cause of death as suicide by drowning, but some sources claim that no water was found in Ludwig's lungs. While these facts could be explained by dry drowning, they have also led to some conspiracy theories of political assassination.
The crown passed to Ludwig's brother Otto I, but since Otto had a clear history of mental illness, the duties of the throne actually rested in the hands of the brothers' uncle, Prince Luitpold, serving as regent.
In 1914, a clash of alliances occurred over Austria-Hungary's invasion of Serbia following the assassination of Austrian Archduke Franz Ferdinand by a Bosnian Serb militant. Germany went to the side of its former rival-turned-ally, Austria-Hungary, while France, Russia, and the United Kingdom declared war on Austria-Hungary and Germany. Initially, in Bavaria and all across Germany, recruits flocked enthusiastically to the German Army. In 1914, Bavaria's corps even accepted the service of one foreigner who would later become infamous in both Bavaria, Germany and the world as a whole, the young Austrian, and future German dictator, Adolf Hitler. Over time, with a stalemated and bloody war on the western front, Bavarians, like many Germans, grew weary of a continuing war. By 1918, civil unrest was spreading across Bavaria and Germany; Bavarian defiance to Prussian hegemony and Bavarian separatism being key motivators. In 1918, the kingdom attempted to negotiate a separate peace with the allies but failed. In November 1918, William II abdicated the throne of Germany, and Ludwig III, along with the other German monarchs, issuing the Anif declaration, followed in abdication shortly afterwards. With this, the Wittelsbach dynasty came to an end, and the former Kingdom of Bavaria became the Free State of Bavaria, which it is still named today.