Mexico had won independence from Spain in 1821. It encompassed not only the current territory, but also Texas, New Mexico, Utah, Nevada, Arizona, California and parts of Colorado. Texas broke off in 1836 (incited by the US) becoming the "Lone Star Republic" and joining the Union in 1845.
In the same year, 1845, James Polk (D) ordered General Taylor to move his troops to the banks of the Rio Grande and offering a challenge to the Mexicans. Polk's declared goal was to acquire California for the US. The border between Mexico and the US was not clear, it was considered the Rio Bravo by the US and the Nueces River (150 miles north)by the Mexicans.
In 1846 Taylor set his camp opposite to the Mexican town Matamoros, in the middle of another former Mexican settlement, whose inhabitants were driven off their land.
Now all that they lacked was an incident. A Colonel of Taylor's camp was found dead, Mexican guerrillas were assumed to have killed him. In addition a patrol got surrounded and wiped out by Mexicans.
This was the long expected reason for a declaration of war.