If you plot the movement of Mars across the Earth's sky it appears to move back on itself during its orbit.
Why does it appear to have a loop shape from Earth?
Astronomy Stack Exchange is a question and answer site for astronomers and astrophysicists. It only takes a minute to sign up.Sign up to join this community
The effect is called apparent retrograde motion.
What happens is that Mars has a 'direction opposite to that of other bodies within its system as observed from a particular vantage point' when this loop occurs.
That's a bunch of words that don't mean a lot to me. A picture is worth a thousand clearer words:
(Imagine this turned sideways and you get the effect in your image)
Basically, because Earth and Mars are orbiting the sun at different rates, our vantage point of Mars changes for each combination of points in the orbit of each planet.
On this scale, the background of stars is pretty much stationary - any apparent movement of the stars due to this effect is going to be negligible. Thus, the stars are our point of reference.
As our vantage point of Mars changes, it appears to shift directions on the stellar background, creating the effect you describe.
And note you'll see the same things with cars on a highway. If your car, in the left lane, is moving faster than a car in the right lane, and both of you are approaching a long arching curve to the left, then look at the other car against the reference of a line of distant trees as you both enter that gradual curve. When the other car is well ahead of you, the fact that it is rounding a corner will make it appear to advance against the distant trees. But as you catch up and pass that car, still in the turn, you will see that car back up against the trees. Then as you get well ahead, again the other car will appear to advance against the trees, if you are still in the curve by then. This is called "parallax."