Why did the Ottoman Empire attempt to reform itself?

When the Ottoman Empire was at its territorial height in the 16th Century, it was the most technologically advanced nation in the world, and was greatly feared by the Christian nations of Europe. However, from then on the empire began to decline relative to the western nations, which advanced significantly in technological and political terms, and succeeded in acquiring large empire in the Americas and Far East. Following the Napoleonic Wars and the rise of nationalism, Greek nationalists started to agitate for independence, which they succeeded in doing with British, French and Russian assistance, while an Albanian general, Mohammed Ali, took control of Egypt, declared independence and conquered much of the Ottoman Empire's Arabian territory, and came close to taking Constantinople. It was only the western powers intervention from preventing the Ottomans being overthrown completely.

After these events, the Ottomans realised they had to reform to catch up to the western world as a major power, and also to guarantee their own empire's independence.

In 1828, Murad III, Sultan committed to reform, as a first step violently purged the Janissaries, the Sultans traditional bodyguard who over the centuries had become unreliable and were against reform and modernisation. He replaced them with a modern professional army based on western models with modern uniforms and equipment, and adopted modern tactics. It was also under his reign that the Tanzimat was enacted, which started in 1839 and was continued by his successor, Abdulmecid.

Over the decades, the Ottomans made a very good job of reforming and modernising. The empire that was nearly destroyed in the 1820s had become a modern nation state, which in 1876 opened its first parliament, albeit with limited powers. Also in this period, they decisively defeated Serbia and Montenegro, crushed the Bulgarian rebels and very nearly defeated Russia one-on-one in the war that followed. However, the aftermath of the war was disastrous for the Ottomans. They only lost due to a lack of unified leadership, also being vastly superior in weaponry and, initially, in numbers.

The Ottomans lost Bulgaria and Bosnia, which provided a third of the Empire's revenues, and Sultan Abdulhamid II then suspended parliament, partly holding them responsible for the defeat and feeling strong government was necessary following the instability that followed defeat.

However, under Abdulhamid's autocracy, local governmental democracy flourished, and the empire continued to invest in education and modernisation of the empire.

His autocracy, however bred resentment, which culminated in the Young Turk revolution, starting in 1908, which resulted in severe instability, which Italy took advantage of to seize Libya, and Greece, Bulgaria, Serbia and Montenegro seized the remaining Ottoman territories in Europe.

Even so, the armed forces were modernised, and it took WWI to dismember the empire, and even then the Allies had a hard time, mainly due to underestimation. In Palestine the Ottomans held off a British army ten times its size for three years, and the Gallipoli campaign was also a success, where a young army officer, Mustafa Kemel (Ataturk) made his name.

After the empire was defeated, he was able to muster an army that drove the Allied and Greek forces completely out of Turkey, and abolished the Sultanate which had failed to support him, and proclaimed the modern Turkish Republic.

In the West, the 19th Century is often seen as a period of terminal decline for the Ottomans, which was the perception at the time, is totally inaccurate.

Thanks to the reform movements, the Ottomans were incalculably stronger at the turn of the 20th Century than they were at the turn of the 19th Century.