Use a frame of reference in which the train is at rest at the moment the ball is thrown. In this frame the ball always has zero horizontal velocity.
Then if the train (and passenger) are accelerating, they move forward in the frame of reference, while the ball stays in the same horizontal position. Relative to the train and passenger, the ball moves backwards.
If the train and passenger are decelerating, they move backward in the frame of reference, while the ball retains the same horizontal position. Relative to them the ball moves forwards.