The correct answer is that in the 1840s, Mexico ceded California to the United States as a part of a peace treaty.
The Treaty of Guadalupe Hidalgo, which ended the Mexican-American War, forced the Mexican government to cede California to the United States, along with the other Interior Provinces (today's New Mexico), in 1848. That same year gold was discovered in California, more precisely in Coloma, near Sacramento, and many people came to the new US territory to look for gold and try their luck.