In the late 19th and early 20th centuries, a shotgun messenger was a private "express messenger" and guard, especially on a stagecoach but also on a train, in charge of overseeing and guarding a valuable private shipment, such as particularly the contents of a strongbox (on a stagecoach) or safe (on a train).
In the American Old West, express messengers of the Wells Fargo company typically carried a short (or sawn-off) 12- or 10-gauge double-barrelled shotgun, loaded with buckshot.
If a stagecoach had only a driver and no messenger, this meant the coach carried no strongbox and was thus a less interesting target for "road agents" (bandits).
To some extent, these weapons were also carried over to use by private guards in trains with strongboxes or safes, where they were also effective.
Like "gunslinger", the actual term "riding shotgun" first appeared in fiction about the Old West, dating back as far as the 1905 book The Sunset Trail by Alfred Henry Lewis.