Rocks become magnetic when

they contain certain minerals that have magnetic properties. This can occur when rocks form from molten magma or when they undergo metamorphism, which involves high pressure and temperature conditions. The presence of magnetic minerals, such as magnetite or lodestone, can cause the rock to exhibit magnetism. Additionally, rocks can become magnetized through the Earth's magnetic field, as certain minerals align with the magnetic field during the rock's formation or over time.