The late nineteenth and early twentieth century is a bit of a mess in terms of alliances. The Ottoman Empire is certainly not alone in allying with a one-time enemy. To give you a few examples, Germany (or rather, Prussia, which became the core of the new German state in 1871) went to war with Austria in 1866, while the French and the British (who were longtime rivals for continental, naval, and, later, colonial power) had briefly allied with the Ottoman Empire in 1853 against Russia, with whom they then came to their famous "Triple Entente" understanding in the early twentieth century. Austria had once controlled most of the Italian peninsula, and fought bitter anti-Italian nationalist actions in 1830-1831, 1848-1849, and also in 1859 (the Second Italian War of Independence), yet Italy signed up to the Triple Alliance, with Austria-Hungary and Germany, in 1881. In 1882, the Dreikaiserbund (Three Emperors' League) was founded between Germany, Russia, and Austria-Hungary. So, in short, alliances were often quite unexpected, and not necessarily predicated upon past friendship (or otherwise.)
Now, to your specific question about the Ottoman Empire. In one sense, we could answer that, while Vienna had benefitted from the decline of the Ottoman Empire, and had grabbed territories in the Balkans, in many ways this worried the policymakers in Constantinople much less than the manoeuvres of their greater and more frequent enemy, Russia. Most conspicuously, the Russians had long been pushing for military control of the Black Sea. This was an existential threat to the Ottoman Empire, since Russian warships in the Black Sea could easily lay siege to Constantinople, since the Bosphorus Straits, which Constantinople straddles, is the entrance to the Black Sea from the Mediterranean.
Here is where things get a little messy. As of August 1914, no decision had been made as to what side of the war (if any) the Ottoman Empire might come down on. However, there were significant practical considerations. The opening moves of the war might have suggested to Constantinople that it might very soon face a formidable threat. The most famous movements of the opening weeks of the war, of course, are the application of the Schlieffen Plan by the Germans through Belgium and France, and the Russian offensive into Eastern Prussia. However, we shouldn't forget the actual flashpoint for the war, in the Balkans; almost immediately, Austro-Hungarian forces began an invasion of Serbia. Though the campaign would eventually prove disastrous for both sides, it forced Russia's hand, and Brusilov launched an offensive of his own, getting as far as Lemberg. If the Russians were to swing southwards to defend their Serbian ally and meet the bulk of the Austro-Hungarian army, this would place a huge number of Russian soldiers basically on the Ottoman doorstep. Few in the Ottoman Empire would have doubted that the Russians would try to take advantage of this situation to at least militarise the coast of the Black Sea, since Russia had been trying to do this since the Crimean War. The Ottoman Empire had been in decline since the nineteenth century, but in the years immediately before the outbreak of the Great War, it had been further weakened by a series of wars that had decimated its territory in Europe and emboldened the newly-independent Balkan states. In September 1911, Italy, hoping to expand its influence in the Mediterranean, launched an invasion of the Ottoman provinces of Tripolitania, Cyrenaica, and Fezzan. Though costly, the campaign was a successful one for the Italians, and within a year they had captured all of what then became Libya, as well as the Dodecanese Islands. This humiliating defeat only encouraged the small Balkan countries, which formed the so-called Balkan League, and jointly declared war on the Ottoman Empire on 8 October 1912. Once more, the Ottomans were defeated militarily, in spite of a coup of army officers hoping to spark a more vigorous war effort. The Ottomans lost nearly every remaining scrap of territory in Europe to the volatile Balkan states of Bulgaria, Rumania, and Serbia; when, in June 1913, Bulgaria felt aggrieved at its lack of gains from its victorious campaign, and declared war on its own allies, the Empire was able to recover the province of Eastern Thrace.
Thus, with the approach of the potentially hostile Russians, the Ottoman leadership could hardly have felt secure, even if Russia had issued guarantees of Ottoman territorial integrity. Even worse (as far as Constantinople was concerned), the large number of ethnic Armenians living in the Russian Empire could make life difficult for the Ottomans, as they could whip up nationalist sentiment among the Armenians living in the Ottoman Empire, thereby creating a dangerous "fifth column" of internal subversives. The empire had been beset with ethnic tensions among the Armenian population for some time; between 1894 and 1896, a series of ethnic cleansing massacres undertaken by the Ottoman Army had resulted in the deaths of approximately 100,000 Armenians, and had helped foster the growth of the Hunchak and Dashnak Armenian revolutionary organisations, which saw themselves naturally aligned to Russia and its client population of Armenians. On the other hand, the empire had close ties with the Germans, which predated the outbreak of war. While Britain and France had their own heavy investments in the Ottoman Empire, Germany’s interests began to increase significantly after two German banks – Deutsche Bank, and the Württembergische Vereinsbank – formed a rail cartel in the Anatolian interior in 1888, with the intention of building a rail network; by 1903, the German government had become officially interested, and began planning in consultation with the Turkish government for what would eventually be called the Berlin-Baghdad Railway. In addition, a military mission led by Lieutenant-General Otto Liman von Sanders had arrived in 1913 in order to assist with the modernisation of the Ottoman army. Eventually Liman von Sanders would become the de facto head of the Ottoman army itself, but before this he had a close working relationship with Enver Pasha, the Ottoman minister of war.
Thus, as danger seemed to present itself, the Ottoman Empire naturally turned to its friend, Germany. As a result, the empire's involvement in the war was not really a case of it allying with Austria-Hungary. More, it was a friendship with Germany, and fear of Russian intentions, that led Constantinople to declare war at the end of 1914.
Sources
Geyikdagi, V. Necla. Foreign Investment in the Ottoman Empire: International Trade and Relations in the Late Nineteenth Century. London. Taurus. 2011.
Kent, Marian (ed.) The Great Powers and the End of the Ottoman Empire. London. Allen & Unwin. 1984.
McMeekin, Sean. The Berlin-Baghdad Express: The Ottoman Empire and Germany’s Bid for World Power. Cambridge, Mass. Belknap Press. 2010.
McMurray, Jonathan S. Distant Ties: Germany, the Ottoman Empire and the Construction of the Baghdad Railway. Westport. Praeger. 2001.
Medlicott, William Norton. The Congress of Berlin and After: A Diplomatic History of Near-East Settlement, 1878-1880. London. Frank Cass. 1963.