The force experienced by the particle is not due to the direction of the field. It is given by the gradient (how the field changes) along the $z$ direction:
$$ F_z =\mu_z \frac{\partial B_z}{\partial z} .$$
Here, $\mu_z$ is the dipole moment of the object, and $B_z$ the magnetic field along $z.$
In the first image, the field $B_z$ is positive along $z$, and it's strength also increases along $z$ because it is more concentrated in the pointy part of the $S$ magnet. Therefore the total gradient is positive along $z$.
In the second image, the field $B_z$ is negative along $z$, but it's strength also decreases along $z$ because it is more concentrated in the pointy part of the $S$ magnet which is now below $N$. Therefore the total gradient is positive because both the field and the way it changes are both negative.
Note: This description is for a classical magnet, but it works for the case of spin $1/2$ because we’re only discussing spin up and spin down. When considering superpositions, the explanation is a bit more involved.