Like almost all the Sherlock Holmes stories authored by Arthur Conan Doyle, this one is presented as a memoir written by Watson, the first-person narrator. The story begins in Holmes and Watson’s Baker Street apartment in London. Holmes, who appears to be deeply engrossed in his chemicals and test-tubes, surprises Watson by apparently reading his mind: "So, Watson...you do not propose to invest in South African securities?" Watson, astonished by Holmes’s remark, demands an explanation, and Holmes complies, relating an intricate chain of reasoning that begins with the presence of chalk on Watson’s left hand the previous night and concludes with his investment decision.
Holmes then hands Watson a sheet of paper bearing some stick figures and asks him what he makes of it. Watson believes it to be a child’s drawing, but Holmes tells him that a client, Mr. Hilton Cubitt, is calling on them soon to seek an explanation of the stick figures drawn on the paper, figures that seem to resemble dancing men. When Cubitt arrives, he explains that he has been married for about a year to a young American woman. He knew little about his wife, Elsie, when they met, and she requested that he not ask her about her past, a past she says she would like to forget. He has honored her request, but recently she seemed quite shaken after receiving a letter from the United States. Shortly after she read and burned that letter, the dancing men hieroglyphics were found written in chalk on the window sill. Cubitt washed them off but noticed his wife’s dismay when he told her about them. Then the paper that Holmes had shown Watson was found on the sundial in the garden. When Cubitt found it and showed it to Elsie, she promptly fainted. He does not wish to violate his promise to his wife and ask whether these dancing men are related to her unknown past, so he has come to Holmes for help in understanding this apparent mystery involving the woman he loves so dearly.
Holmes asks Cubitt some questions about the neighborhood and sends him home, asking him to watch for more dancing men drawings and urging Cubitt to copy down faithfully any that he finds. Holmes studies the drawing silently and makes no remarks about the case to Watson. About two weeks later, Cubitt returns with more hieroglyphics; some have been written in chalk on a door, others have been scrawled on a paper left on the sundial. One night, Cubitt reports, he saw a figure moving through the darkness in the yard; he took his pistol and, despite his wife’s protests, went after the man. He did not find anyone, but the next morning more dancing men, apparently drawn by this mysterious visitor, were found chalked on the door. Cubitt believes that his wife possibly knows who this man is; he remains true to his promise, however, and refuses to interrogate her about the matter.
Cubitt returns to his home—Riding Thorpe Manor—on the train, and Holmes puzzles over the drawings some more. When Cubitt mails him another set of drawings found on the sundial, Holmes examines them and decides that he has the key to the mystery and needs to visit Norfolk immediately. When he and Watson arrive at Riding Thorpe Manor, they find Inspector Martin of the local police. Martin reports that Cubitt has been shot dead; Mrs. Cubitt has also been shot but remains alive in critical condition. It is assumed that Mrs. Cubitt murdered her husband and then attempted to take her own life. Holmes is stunned by this news and immediately examines the scene of the crime and questions the staff about the shooting. After close scrutiny of the room where the murder took place, Holmes discovers that three shots were fired, rather than only two as the police assume. Because only two bullets were fired from the revolver found with the bodies, Holmes concludes that another person with a gun was present at the time of the murder. The third person perhaps fired into the house through an open window, he reasons, a thesis confirmed by a spent cartridge found in the garden.
Testimony from the staff that they heard a loud explosion, followed by a second explosion not nearly so loud, establishes that there were two shots fired almost simultaneously, followed shortly by a single third shot.
Holmes questions the stable boy about the inns and farms in the area and then sits down and writes a brief note, which he sends off to a nearby farm, addressed to a Mr. Abe Slaney. Holmes remarks that they will have about an hour before anything happens; he uses the time to explain the secret of the dancing men. The individual dancing figures are symbols substituted for letters of the alphabet. Because the most frequently used letter in the English alphabet is "E," Holmes assumed that the dancing man hieroglyph that appeared most often stood for the letter "E." By substituting other letters for symbols, using the frequency with which these letters normally appear in the language as a guideline, Holmes was able to break the code and decipher the messages. Finding the name Abe Slaney in one of the messages, and, knowing that the original letter that had disturbed Mrs. Cubitt had come from the United States, Holmes then cabled New York to ask a police friend there whether Abe Slaney was known to him. When the response indicating that Slaney was "the most dangerous crook in Chicago" came on the same night that Holmes received the last dancing man message from Cubitt, a message saying "Elsie prepare to meet thy God," Holmes realized that there was no time left to waste. Unfortunately, he explains, he and Watson arrived too late to prevent a murder.
Holmes’s explanation is interrupted by the appearance of Slaney, who is immediately apprehended. He was summoned to the house by Holmes’s message, written in dancing man hieroglyphics; Slaney had assumed that Elsie Cubitt had sent the message. The dancing man code was developed in Chicago by Elsie’s father, the leader of the criminal gang to which Slaney belonged. Elsie was at one time engaged to marry Slaney but decided that she wanted no part of his criminal life, so she fled the country and attempted to start over in England, marrying Cubitt, who agreed to raise no questions about her past. When Slaney finally tracked her down and began to leave her messages in the code she had learned as a child, she panicked. She wrote him and asked him to leave, then asked him to meet her so she could attempt to bribe him into disappearing.
It was during this late-night meeting that Hilton Cubitt surprised his wife and Slaney arguing. Slaney and Cubitt exchanged shots, but Slaney ran without knowing whether Cubitt had been seriously wounded. Elsie Cubitt, unknown to Slaney, shot herself with her husband’s revolver when she saw that her secret past had indirectly brought the life of her trusting husband to an end.
After explaining how the pieces of the puzzle fit together, Holmes hurries Watson off for the train back to London. Slaney is given a life sentence, and Mrs. Cubitt survives her self-inflicted wound to live out her life as a despondent widow.