Lang placed an advert in a newspaper in 1930 stating that his next film would be Mörder unter uns (Murderer Among Us) and that it was about a child murderer. He immediately began receiving threatening letters in the mail and was also denied a studio space to shoot the film at the Staaken Studios. When Lang confronted the head of Staaken studio to find out why he was being denied access, the studio head informed Lang that he was a member of the Nazi party and that the party suspected that the film was meant to depict the Nazis. This assumption was based entirely on the film's original title and the Nazi party relented when told the plot.
M was eventually shot in six weeks at a Staaken Zeppelinhalle studio, just outside Berlin. Lang made the film for Nero-Film, rather than with UFA or his own production company. It was produced by Nero studio head Seymour Nebenzal who later produced Lang's The Testament of Doctor Mabuse. Other titles were given to the film before "M" was chosen; Eine Stadt sucht einen Mörder (A City searches for a Murderer) and Dein Mörder sieht Dich an (Your Killer Looks At You). While researching for the film, Lang spent eight days inside a mental institution in Germany and met several child murderers, including Peter Kürten. He used several real criminals as extras in the film and eventually 25 cast members were arrested during the film's shooting. Peter Lorre was cast in the lead role of Hans Beckert, acting for the film during the day and appearing on stage in Valentine Katayev's Squaring the Circle at night.
Lang did not show any acts of violence or deaths of children on screen and later said that by only suggesting violence he forced "each individual member of the audience to create the gruesome details of the murder according to his personal imagination".
M has been said, by various critics and reviewers, to be based on serial killer Peter Kürten—the "Vampire of Düsseldorf"—whose crimes took place in the 1920s. Lang denied that he drew from this case, in an interview in 1963 with film historian Gero Gandert; "At the time I decided to use the subject matter of M there were many serial killers terrorizing Germany—Haarmann, Grossmann, Kürten, Denke, [...]".
M was Lang's first sound film and Lang experimented with the new technology. It has a dense and complex soundtrack, as opposed to the more theatrical "talkies" being released at the time. The soundtrack includes a narrator, sounds occurring off-camera, sounds motivating action and suspenseful moments of silence before sudden noise. Lang was also able to make fewer cuts in the film's editing, since sound effects could now be used to inform the narrative.
The film was one of the first to use a leitmotif, a technique borrowed from opera, associating a tune with Lorre's character, who whistles the tune "In the Hall of the Mountain King" from Edvard Grieg's Peer Gynt Suite No. 1. Later in the film, the mere sound of the song lets the audience know that he is nearby, off-screen. This association of a musical theme with a particular character or situation is now a film staple. Peter Lorre could not whistle and Lang's wife and co-writer Thea von Harbou is heard in the film.