Esther G. answered 12/06/20
MIT Physics Graduate with 10+ Years of Physics Tutoring Experience
When you apply a force to the glass, the glass begins accelerating. The water in the glass isn't directly experiencing this force, and so doesn't accelerate forward - it therefore appears to move backwards.
Since the wall of the glass is solid, normal forces keep the water within the glass, eventually accelerating the water with the glass.
When the glass comes to a stop, the opposite effect happens - no force is directly acting on the water, so it wants to continue moving with its initial velocity. As a result, it sloshes forward into the now-stationary glass wall.