This cartoon by Bert Thomas (1883-1966) for the British Ministry of Information during World War II illustrates the folk etymology of the phrase.
The earliest known mention of this phrase is in a letter published by the London literary magazine The Athenæum of 8th August 1919:
The expression “Put a sock in it,” meaning “Leave off talking, singing or shouting,” should be included in the lists of “Slang in War-Time.”
I am, Sir, yours obediently.
The same year, on 18th October, in his column Passing Notes, ‘Mercurius’ wrote the following in the Hobart (Australia) newspaper The Mercury:
Only this week I came upon a letter to something like the following effect: “[…] Hope you are in the pink, Old Bean, and not feeling too much of an onion at home! Did you click for a rise? The gadget is to barge in on the Chief right away — at the double, in fact. Cold feet are no good for bringing in the dough. If the Chief is inclined to jib, tell him to put a sock in it! These old buffers cut no ice with me, and you put your buttons on Percy that he won’t get wind up.”
The probably made-up letter quoted by ‘Mercurius’ was published the same year by the Wellington (New-Zealand) newspaper The Evening Post of 22d November, in an article titled Is it English? The language as it is spoken.
It was also published in Britain by The Educational Times, A Review of Ideas and Methods of January 1920, with the following explanation:
Sock [to put a — in it]: i.e. in his mouth — to cease talking.
In War Slang, American Fighting Words and Phrases since the Civil War (second edition – 2004), in the chapter World War I, Paul Dickson gave the same explanation:
Put a sock in it: be quiet (as if one had a sock stuffed into one’s mouth).
However, as early as 1925, in Soldier and Sailor Words and Phrases, Edward Fraser and John Gibbons gave the origin that has since been popular:
Put a sock in it: Leave off making a noise. Stop talking. (Suggested by the handiest method of gagging a gramophone).
But, according to B. A. Phythian in A Concise Dictionary of Phrase and Fable (1993):
This seems improbable: in the sort of household that alone could have afforded such a novelty [= the gramophone] it is unlikely that a sock would be used in the drawing-room.
And he explains:
In a barrack-room, however, socks would certainly be lying around at night and one can imagine a heavy snorer being shouted at and told to ‘put a sock in it’ (in his mouth). Some such military origin is far more likely.