The early history of the fez is shrouded in mystery; as the name suggests, a common theory suggests that the felt hat originated in Morocco and was brought over to the Ottoman Empire in the early nineteenth century, though other scholars have pointed out that the design bears similarities to traditional headgear worn by Balkan peasants.[1] At any rate, the fez's popularity at the turn of the century (which continued until the reform of Atatürk mentioned in another comment) is traceable to a single source: the influential patronage of Sultan Mahmud II (1808-39) and his many efforts on the hat's behalf.
The standard academic interpretation of the introduction of the fez and its symbolism is Donald Quataert's "Clothing Laws, State, and Society in the Ottoman Empire, 1720-1829."[2] This article has informed all subsequent discussions of the subject (at least that I have seen). Quataert contextualizes the adoption of the fez as part of what he calls the "Clothing revolution of Mahmud II," a set of reforms designed to relegitimate an Ottoman monarchy struggling with challenges to its authority in the Balkans (see: the then-ongoing Greek Revolution) and Egypt (see: Mehmet Ali). The fez—at this point wrapped in cloth—made its first officially-sanctioned appearance in the new army corps Mahmud constructed after disbanding the Janissaries in the "Auspicious Incident" (1826), as a symbol of the new force's disconnection from its unruly predecessor; the plain fez was legislated as a uniform for Mahmud's civil bureaucracy a few years later, in 1829. This marked an important shift in Ottoman clothing law—where hats were previously important markers of social, religious, and bureaucratic distinction, now all officials were ordered to wear the same headgear. The fez thus represented a commitment to the equality of all Ottoman subjects (regardless of religion) before the sultan, a measure anticipating the reforms of the Tanzimat period (1839-76); and with the sultan himself adopting the hat, as he did in his trips to the empire's provinces (memleket gezileri), it became a mark of liberal Ottoman affiliations.[3]
So here I'd like to modify your question a bit; Turks wore fezzes, of course, but they were far from the only peoples to do so in the multinational Ottoman Empire.[4, 5] In fact, some of the most enthusiastic early adopters of the headgear were non-Muslims (some of whom may have been ethnic Turks, but many of whom were not) jumping at the opportunity to abandon their old mandated distinguishing headgear. A notable exception was the artisan and worker class, in which Turks and non-Turks alike pointedly refused to adopt the new hats. Two theories have tried to explain this opposition, tying it either to religious conservatism (and opposition to the breaking down of barriers between the empire's religious classes) or political-economic conservatism (and opposition to Mahmud's laissez-faire policies that threatened workers' economic security). When workers did adopt the fez, they wrapped it in colorful fabric as a way of signalling nonidentity with the plain-fez-wearing bureaucrats and civil servants and, at the same time, performing an artisanal identity that crossed religious and ethnic lines (Quataert's article has some great pictures of this practice, which continued through the 19th century.)
While Mahmud's 1829 code largely foundered—the attempt to introduce a minimalistic dress code along European lines was ahead of its time as far as Ottoman sensibilities were concerned—the fez endured as a symbol of liberal, multicultural Ottoman identity. And slowly those signifiers faded, and it just became a symbol of Ottoman identity. Eventually, the fez even transcended the Ottoman Empire's borders to become a sign of modern Islam in other countries, like India—an ironic departure from the initially fiercely secular ideas tied to the headgear.[1, 3b]
SOURCES AND CITATIONS
[1] Margrit Pernau, "Shifting Globalities—Changing Headgear: The Indian Muslims Between Turban, Hat, and Fez, in Translocality: The Study of Globalizing Processes from a Southern Perspective, edited by Ulrike Freitag and Achim von Oppen (Leiden: Brill, 2010), 249-267.
[2] Donald Quaetert, "Clothing Laws, State, and Society in the Ottoman Empire, 1720-1829," International Journal of Middle East Studies, Vol. 29, No. 3 (Aug., 1997), pp. 403-425 [JSTOR link]
[3] Darin Stephanov, "Sultan Mahmud II (1808-1839) and the First Shift in Modern Ruler Visibility in the Ottoman Empire," Journal of the Ottoman and Turkish Studies Association, Vol. 1, No. 1-2 (2014), pp. 129-148. [JSTOR link]
[4] For example, Ottoman Kurds readily adopted the fez as a symbol of cosmopolitan Ottoman civilization, though not to the complete exclusion of their older, more traditional costume. See Ahmet S. Aktürk, "Fez, Brimmed Hat, and Kum û Destmal: Evolution of Kurdish National Identity from the Late Ottoman Empire to Modern Turkey and Syria," Journal of the Ottoman and Turkish Studies Association, Vol. 4, No. 1 (May 2017), pp. 157-187. [JSTOR link]
[4b] Aktürk notes in a footnote (citing Hanioğlu's Brief History of the Late Ottoman Empire?) that the fez's identification with Muslims within the Ottoman empire came as a result of the tension caused by Christian nationalist agitation and Abdülhamid II's resultant re-privileging of Ottoman Muslims. (164n17)
[5] For a brief discussion of the fez and its symbolism among Late Ottoman and post-Ottoman Jews, see Devin S. Naar, Turkinos beyond the Empire: Ottoman Jews in America, 1893 to 1924, The Jewish Quarterly Review, Vol. 105, No. 2 (Spring 2015), 203. [JSTOR link]