He assumed the presidency for the first time after general Domingo Nieto's death for a short period in 1844, then in 1845 until 1851, again from 1855 to 1862 and, finally, during a brief period in 1863.
Castilla became a prisoner of war, but managed to escape and returned to Peru in 1821, deserting the Spanish Army and offering his services to José de San Martín, who enrolled him in the Patriot Army with the rank of lieutenant (a rank he had held with the Spanish Army). When San Martin resigned as "Protector of Peru", Castilla sided with José de la Riva Agüero, who in turn shortly became president in 1823.
In 1824, when the Peruvian Congress named Simón Bolívar dictator or "Liberator of Peru", Castilla joined Bolivar's army, fighting in the decisive Battle of Ayacucho, which helped Peru gain its independence from Spain. In 1825 he was named governor of his native province of Tarapacá. In 1833, Castilla married Francisca Diez Canseco.
Soon after, Castilla, along with Domingo Nieto, overthrew Manuel Ignacio de Vivanco during the Battle of Carmen Alto. Nieto assumed the presidency but died a few months later. Castilla assumes the position on February 17, 1844 until December 11 of that year. After defeating the other caudillos around the country, Castilla reinstated Menéndez as President, in order to achieve a constitutional transition to democracy.
In 1845, Castilla won the Peruvian presidential elections and was sworn in April of that year. During this time the guano export boom was rapidly expanding, largely due to treaties signed with the British company Anthony Gibbs, which had commercialized the guano in Europe. Important urban projects were also begun under this period, such as the first railroad from Lima to Callao, which helped in the transportation of the guano from the production centers ready to be shipped abroad.
After six years in power, Castilla was succeeded by José Rufino Echenique. In 1854, however, another rebellion was led in Peru's second-largest city, Arequipa, by Castilla himself, who was largely urged by other Peruvian liberals to help in the suppression of slavery in the country.
On December 3, 1854, in the city of Huancayo, an abolition of slavery law was passed. As this law was being applied, Castilla confronted and defeated Echenique in the Battle of La Palma on January 5, 1855.
In 1859, Peru had a confrontation with neighboring Ecuador over disputed territory bordering the Amazon. Though Peru was considered successful, Castilla failed to secure a definitive agreement with Ecuador and the issue would haunt both countries until the end of the 20th century when the 1997 Peace and Border Treaty of Itamaraty was signed by Peru and Ecuador in Brazil. In December 1860 a new constitution was enacted during Castilla's presidency and became Peru's supreme law until 1920. Castilla's second presidency, therefore, was marked by the liberation of slaves and indigenous Peruvians, as well as a new postal system and a new constitution.
In 1862 he was succeeded by Miguel de San Román, who died less than a year later, prompting Castilla to claim the presidency and refused to recognise Pedro Diez Canseco, the Second Vice President of the Republic. Diez Canseco, however, was chosen as interim president from April to August 1863, and was succeeded by Juan Antonio Pezet.
In 1864 Castilla condemned the international policies of Pezet, only to be jailed and exiled to Gibraltar. During his absence the historic Battle of Callao took place, which became Spain's final and unsuccessful move to reconquer independent Peru.
After he returned to Peru, he was again deported to Chile on the orders of then president Mariano Ignacio Prado. In a last effort to regain power for a fifth time, Castilla – now nearly seventy – and a group of followers landed in Pisagua and proceeded towards the Tiviliche desert. This last try, however, proved fatal and Castilla died in his final attempt to pass through southern Peru on May 30, 1867.