In 1596 a bigger army of the Turks attacked Eger, and took the castle after a short siege. Eger then fell under Turkish rule for more than eighty years and became the capital of a Turkish vilayet (administrative division). The Turks transformed churches into mosques, rebuilt the castle, and built other structures including public baths and minarets.
After the Turks' failed attempt to conquer Vienna, the Habsburgs, who controlled the rest of Hungary, began to drive the Turks out of the country. The Christian army led by Charles of Lorraine occupied the castle of Buda in 1686 and starved the castle of Eger into surrender in 1687.
Eger began to prosper again. The bishops reclaimed the town, causing many of the Protestant inhabitants to leave. During the 1703-1711 war of independence against the Habsburgs, the town supported the Hungarian leader Prince Francis II Rákóczi, but the Imperial army defeated the Hungarians, and soon after that Eger suffered from a plague. During the 1700s many people immigrated to Eger, and between 1725 and 1750 the population has risen from 6000 to 10,000. New buildings were built in Baroque (and later in Zopf) style, including the cathedral, the Episcopal Palace, the county hall, the college (now called Eszterházy College after its founder) and several churches. Also the mosques were transformed into Catholic churches.