In 1845, during the annexation of Texas, President James K. Polk dispatched Taylor to the Rio Grande in anticipation of a battle with Mexico over the disputed Texas–Mexico border. The Mexican–American War broke out in April 1846, and Taylor defeated Mexican troops commanded by General Mariano Arista at the battles of Palo Alto and Resaca de la Palma, driving Arista's troops out of Texas. Taylor then led his troops into Mexico, where they defeated Mexican troops commanded by Pedro de Ampudia at the Battle of Monterrey. Defying orders, Taylor led his troops further south and, despite being severely outnumbered, dealt a crushing blow to Mexican forces under General Antonio López de Santa Anna at the Battle of Buena Vista. Taylor's troops were transferred to the command of Major General Winfield Scott, but Taylor retained his popularity.
The Whig Party convinced a reluctant Taylor to lead its ticket in the 1848 presidential election, despite his unclear political tenets and lack of interest in politics. At the 1848 Whig National Convention, Taylor defeated Winfield Scott and former Senator Henry Clay for the party's nomination. 