The predecessor of Komárom Esztergom County, Komárom county and Esztergom county were founded by Stephen I of Hungary. Both counties had parts that now belong to Slovakia. Throughout their history the borders of the two counties were frequently modified, and they were merged several times as well. When the castle of Esztergom was captured by the Ottomans in 1543, the leadership of the county fled from there. The castles of Érsekújvár, Komárom, and Tata were the three border castles stopping the Ottoman conquest for long decades. During the Ottoman occupation of Hungary, Esztergom became the center of a sanjak spanning across several counties. Komárom county on the other hand was continuously functioning, its castle was never captured by the conquering armies. The general assembly of nobles in Esztergom county was reintroduced after the liberation of the city from the Ottomans, by armies led by John III Sobieski in 1696. The assembly was dissolved in both counties during Rákóczi's War of Independence, and then it was reformed after the war, in Esztergom county in 1710 and in Komárom county in 1712.