Any object with a non-spherical moment of inertia tensor that is subject to a non-uniform gravitational field will experience a torque due to the gradient of the gravitational field. This can result in a benefit or a challenge for artificial satellites.
The Earth has a non-spherical moment of inertia tensor due to its equatorial bulge, and the Moon's and the Sun's gravitational field are non-uniform (gravitational acceleration more or less follows an inverse square law). This means the Earth is subject to instantaneous gravity gradient torques from the Moon and the Sun.
These torques would average out to zero if the Earth's equator was aligned with the Earth's orbital plane about the Sun and the Moon's orbital plane about the Earth and if the orbits were circular. Since this isn't the case, residual torques exist after averaging (better: integrating) the instantaneous gravity gradient torques over time. It is these averaged / integrated torques that result in the axial precession.