During the Mexican–American War, as colonel of the 2nd U.S. Infantry, Riley fought at the Siege of Veracruz and the Battle of Cerro Gordo, where he was cited for bravery. He was brevetted brigadier general and assumed command of the 2nd Brigade in David E. Twiggs's Second Division. He led his brigade at the Battle of Contreras and the Battle of Churubusco, where Winfield Scott gave him credit for the U.S. victory: Riley had discovered a way around the rear of Velencia's position. He was appointed brevet major general and fought at the Chapultepec. After the battle at Churubusco, he also presided over the courts-martial of 72 deserters of the so-called Saint Patrick's Battalion discovered hiding in the San Patricios convent; among them were John Murphy and John Riley. He was generally considered one of the ablest brigade commanders in the army during the war with Mexico.