Spanish attempts to reconquer Mexico
Mexican independence was officially achieved on September 27, 1821, under the Treaty of Córdoba. Spain did not recognize the treaties, arguing that the viceroy Juan O'Donojú had no authority to recognize the independence of any overseas province. This situation was dangerous to the newly acquired independent status of the nation, which had not yet been recognized by any of the European powers that could support it, and the threat of Spanish reconquest was a constant worry to the leaders of the nascent regime. On May 13, 1822 decrees were issued by the government to imprison anyone who conspired against Mexico's independence.
History of Mexico City
When rebellion against Spanish rule broke out, interests outside of Mexico City would be represented by Father Miguel Hidalgo y Costilla, José María Morelos and others. While the nobility in Mexico City also did not like the absolute colonial system, their goal was limited representation and autonomy within the Spanish empire. They decided to make their stand in 1820, after the rural insurgency had been going on for several years, choosing Colonel Agustín de Iturbide to push their interests militarily. Iturbide had fought against Morelos between 1813 and 1816. However, between 1816 and 1820, Iturbide was becoming sympathetic to the idea of some degree of independence for Mexico. In 1821, Iturbide was the supreme commander of the royalist forces and had put down all but one of the major rebels, Vicente Guerrero. Iturbide decided to meet with Guerrero, after becoming convinced that independence was the only real course for Mexico. However, Iturbide's idea was a Mexican monarchy with ties to King Ferdinand VII of Spain.
After switching sides, Iturbide chose to pressure the colonial government by repeating Hidalgo's strategy of closing in on the city from the surrounding area. Iturbide was able to succeed where Hidalgo had not because the Spanish-born commanders in the city supported Iturbide's idea of limited autonomy, and many the royalist forces were in the field battling insurgents like Guerrero. Iturbide's Army of the Three Guarantees (Independence, Union, Religion) entered Mexico City on 21 September 1821. On the following 27 September, Mexico was declared independent.
The Mexico City nobles sought to preserve as much of the old as possible, and garnered the support of a substantial section of the royalist army to recreate central power. Their objective was to halt the devolution of power to the regions outside the city and the lower echelons of society. Shortly after his triumphant entrance into the city, Iturbide declared what is now known as the First Mexican Empire, with himself as emperor, from the palace that now bears his name. The coronation of Agustín as emperor and his wife Ana María as empress took place amid much pomp and circumstance on 21 July 1822 at the Cathedral of Mexico City. The Archbishop Fonte presided over the anointment of the Emperor who following Napoleon's example, crowned himself.
Following his coronation, the new empire was politically and financially unstable. Iturbide was accused of taking too much power for himself, and his main rival was Antonio López de Santa Anna. In the spring of 1823, Iturbide offered his abdication, which was accepted by his political opponents and then left the country for Europe. 
Mexico was then declared a republic. The republican constitution of 1824 established Mexico City as the nation's capital. Unrest followed for the next several decades, as different factions fought for control of Mexico.