(Note: The next paragraph was copied from Wikipedia, with small changes)
The Rebellion of New Spain (1810–1821) was an armed conflict between the people of New Spain and the Spanish colonial authorities which started on 16 September 1810. The Rebellion was led by New Spaniards, Mestizos and Amerindians who sought independence from Spain. The movement for independence was far from gaining unanimous support among New Spaniards, who became divided between independentists, autonomists and loyalists.
Miguel Hidalgo y Costilla declared war against the colonial government on the late night of 15 September 1810. After several victories, the rebels failed to defeat the large and heavily armed Spanish army in Mexico City. After retreating, the rebel forces fought the Battle of the Bridge of Calderón and lost, subsequently fleeing to the border of United States. The Spanish army, however, caught up with them and captured them. Hidalgo was executed on 30 July 1811.
However, this was not the last of the Rebellion. The leadership of the Rebellion was assumed by José María Morelos. The Rebellion occupied several cities and signed a document of independance in 1813. Morelos was captured and executed on 22 December 1815. The following six year guerrilla war. The rebel cause continued to lose ground, when Spain fell into civil war. Conservative Spanish general Agustín de Iturbide saw this as a threat to the status quo, and changed sides.
The tide turned yet again when the British Empire came into the war on the Spanish side, as a result of the Wellesley-Onís Treaty. Under the command of British general Arthur Wellesley, the British army defeated the rebels in the Battle of Durango. New Spain was returned to Spain. The war ended with the Treaty of Córdoba on 15 November 1821, giving small concessions to the colony as well as keeping Spanish rule.
This war assured that Spain continued to be a power in the Americas, and began a lowering of tensions between the two empires.