Castellanos was born in Montilla, Córdoba, Spain. After completing studies as a Cadet in the Infantry Corps School (later Academy) of Toledo, he was posted in 1865 to Cuba where he joined the garrison of Puerto Príncipe (Camagüey).
He married a Camagueyan, with whom he went on to have six children.
By 1868, with the outbreak of the Ten Years' War, Castellanos already held the rank of Captain. During the war, he assisted in military actions within the Central Department, the largest of the three regions into which the island was divided.
Other campaigns followed for Castellanos: the Protest of Baragua and the Little Wars. Between 1883 and 1886, he was Secretary of Sub-Inspection of Infantry and Militia in Havana, after which he returned to Spain upon his wife's death.
Widowed and with six children, Castellanos spent the following nine years in charge of the Campaign section of the War Ministry in Madrid. He held this position until 1895 when Cuba's War of Three Years began. Captain General of the Island Arsenio Martínez Campos called upon Castellanos for his immediate presence as a strategist with knowledge of the country, accumulated seniority and experience in war and Cuba’s circular campaigns.
Castellanos took command of the Port-au-Prince Commandant and participated in campaigns throughout the territory. In April 1898, he overtook the Trocha Division. With American intervention in the conflict, land battles in Santiago took place, as well as the sinking of the Cervera squadron in the bay, after which the Spanish surrendered. Castellanos handled the repatriation of the troops from Trochas until November, when he took over as Captain General in Havana.
After the battles, Castellanos attended to the repatriation of the 87,000 Spanish soldiers on the island. He personally ensured good behavior, accommodations and provisioning, and oversaw the return of those who had been hospitalized. As the Spanish President of the Evacuation Commission, he slowed the American move to take over Cuba.
On 1 January 1899, Castellanos officially ("in the name of his King") transferred the island to the United States:Sir, in compliance with the Treaty of Paris, as agreed by the military commissions of the Island and the orders of my King, at noon today, 1 January 1899, Spanish sovereignty over Cuba ceases to exist and that of the United States begins ...
He embarked on the steamship Rabat to travel to Matanzas and Cienfuegos to complete the repatriation. On February 6, he returned to Spain with the last contingent of troops on the steamship Catalonia.
Castellanos became the youngest Lieutenant General in the Spanish Army. In succession he held the command of the Captaincy of New Castile and Extremadura, New Castile, Galicia, Old Castile and Valencia, until 1910 when he was appointed Adviser to the Supreme Council of War and Navy.
He retired at the age of 72 in 1916; he died at age 85 in Madrid on the 18th of January, 1929.