The Doppler Effect. When a train is approaching, the sound waves in front of it are "squished" and the sound of a train whistle will seem to be a higher pitch than the actual pitch of the whistle. If the train is departing, the sound waves will appear to be "stretched" and the sound will be lower than the actual pitch of the whistle. As the train passes by, the sound will shift from the higher pitch to the lower pitch, passing its true sound on the way.
Hope this helps! :)