Mexico, then known as New Spain, gained its independence from Spain between 1810 and 1821. At this time, Mexico was a Spanish colony populated mostly by a large number of indigenous Americans (Amerindians), mestizos (those of mixed Iberian-Indigenous American heritage), the powerful land-owning criollos (those of Iberian heritage born in Mexico), and a small number of African-descent and mulatto (those of mixed Iberian-African heritage) populations. The criollos held most of the political power in Mexico.
In 1810, Father Miguel Hidalgo y Costilla (1753–1811), a criollo priest, staged a revolt known as the Hidalgo Revolt. Even before this year, he participated in a criollo attempt to overthrow Spanish military officers who were interfering with the civilian administration in Mexico. This earlier attempt informed Hidalgo’s popular revolt in 1810, when he and his associates declared themselves in favor of Fernando VII, the previous Spanish king who had been deposed by Napoleon. In some ways, therefore, the revolt was a response to French control over Spain.
Hidalgo and his associates, made up of criollos, mestizos, indigenous Americans, mulattos, and those of African descent, marched on the city of Guanajuato in south-central Mexico. Though they managed to defeat some of the Spanish troops sent against them, they were eventually defeated in 1811, and Hidalgo was executed. His associates continued to fight against the royalists loyal to Spain. This launched a civil war known as the War of Independence. The war ended nearly a decade later, in 1821, when the independence-fighters (nationalists) compromised with the royalists and Mexico was granted independence. During the war, the nationalists designed a constitutional government and a constitution (1812), abolished slavery, and declared all of those born in Mexico, regardless of their ethnicity, to be Americans.
There is some debate over whether those who led the initial revolt in 1810 were intentionally launching an anti-colonial struggle and actually sought separation from Spain, or whether they were seeking self-government and increased political rights. For more on that, you should refer to Jaime E. Rodriguez O.’s book, “We Are Now the True Spaniards”: Sovereignty, Revolution, Independence, and the Emergence of the Federal Republic of Mexico, 1808-1824.