Wagner's autobiography Mein Leben (My Life) described the genesis of Die Meistersinger. Taking the waters at Marienbad in 1845 he began reading Georg Gottfried Gervinus' Geschichte der deutschen Dichtung (History of German Poetry). This work included chapters on mastersong and on Hans Sachs.
I had formed a particularly vivid picture of Hans Sachs and the mastersingers of Nuremberg. I was especially intrigued by the institution of the Marker and his function in rating master-songs ... I conceived during a walk a comic scene in which the popular artisan-poet, by hammering upon his cobbler's last, gives the Marker, who is obliged by circumstances to sing in his presence, his come-uppance for previous pedantic misdeeds during official singing contests, by inflicting upon him a lesson of his own.
Gervinus' book also mentions a poem by the real-life Hans Sachs on the subject of Protestant reformer Martin Luther, called Die Wittenbergisch Nachtigall (The Wittenberg Nightingale). The opening lines for this poem, addressing the Reformation, were later used by Wagner in act 3 scene 5 when the crowd acclaims Sachs: Wacht auf, es nahet gen den Tag; ich hör' singen im grünen Hag ein wonnigliche Nachtigall. (Awake, the dawn is drawing near; I hear, singing in the green grove, a blissful nightingale)
In addition to this, Wagner added a scene drawn from his own life, in which a case of mistaken identity led to a near-riot: this was to be the basis for the finale of act 2.
Out of this situation evolved an uproar, which through the shouting and clamour and an inexplicable growth in the number of participants in the struggle soon assumed a truly demoniacal character. It looked to me as if the whole town would break out into a riot...Then suddenly I heard a heavy thump, and as if by magic the whole crowd dispersed in every direction...One of the regular patrons had felled one of the noisiest rioters ... And it was the effect of this which had scattered everybody so suddenly.
This first draft of the story was dated "Marienbad 16 July 1845". Wagner later said, in Eine Mitteilung an meine Freunde (1851) (A Communication to my Friends)  that Meistersinger was to be a comic opera to follow a tragic opera, i.e. Tannhäuser. Just as the Athenians had followed a tragedy with a comic satyr play, so Wagner would follow Tannhäuser with Meistersinger: the link being that both operas included song-contests.
Influence of Schopenhauer
In 1854, Wagner first read Schopenhauer, and was struck by the philosopher's theories on aesthetics. In this philosophy, art is a means for escaping from the sufferings of the world, and music is the highest of the arts since it is the only one not involved in representation of the world (i.e. it is abstract). It is for this reason that music can communicate emotion without the need for words. In his earlier essay Oper und Drama (Opera and Drama) (1850–1) Wagner had derided staples of operatic construction: arias, choruses, duets, trios, recitatives, etc. As a result of reading Schopenhauer's ideas about the role of music, Wagner re-evaluated his prescription for opera, and included many of these elements in Die Meistersinger.
Although Die Meistersinger is a comedy, it also elucidates Wagner's ideas on the place of music in society, on renunciation of Wille (Will), and on the solace that music can bring in a world full of Wahn (delusion, folly, self-deception). It is Wahn which causes the riot in act 2 — a sequence of events arising from a case of mistaken identity, which can be seen as a form of self-delusion. Commentators have observed that in his famous Act 3 monologue Wahn, Wahn, überall Wahn (Madness! Madness!, Everywhere madness!), Sachs paraphrases Schopenhauer's description of the way that Wahn drives a person to behave in ways that are self-destructive:
"in Flucht geschlagen, wähnt er zu jagen; hört nicht sein eigen Schmerzgekreisch,
wenn er sich wühlt ins eig'ne Fleisch, wähnt Lust sich zu erzeigen!"
"driven into flight he believes he is hunting, and does not hear his own cry of pain:
when he tears into his own flesh, he imagines he is giving himself pleasure!"
Following the completion of Tristan und Isolde, Wagner resumed work on Die Meistersinger in 1861 with a quite different philosophical outlook from that which he held when he developed his first draft. The character of Hans Sachs became one of the most Schopenhauerian of Wagner's creations. Wagner scholar Lucy Beckett has noted the remarkable similarity between Wagner's Sachs and Schopenhauer's description of the noble man:
We always picture a very noble character to ourselves as having a certain trace of silent sadness... It is a consciousness that has resulted from knowledge of the vanity of all achievements and of the suffering of all life, not merely of one's own. (Schopenhauer: Die Welt als Wille und Vorstellung – The World as Will and Representation)
The other distinctive manifestation of Sachs's character – his calm renunciation of the prospect of becoming a suitor for Eva's love – is also deeply Schopenhauerian. Sachs here denies the Will in its supposedly most insistent form, that of sexual love. Wagner marks this moment with a direct musical and textual reference to Tristan und Isolde: Mein Kind, von Tristan und Isolde kenn' ich ein traurig Stück. Hans Sachs war klug und wollte nichts von Herrn Markes Glück. ("My child, I know a sad tale of Tristan and Isolde. Hans Sachs was clever and did not want anything of King Marke's lot.")
Completion and premiere
Having completed the scenario, Wagner began writing the libretto in 1862, and followed this by composing the overture. The overture was publicly performed in Leipzig on 2 November 1862, conducted by the composer. Composition of act 1 was begun in spring of 1863 in the Viennese suburb of Penzing, but the opera in its entirety was not finished until October 1867, when Wagner was living at Tribschen near Lucerne. These years were some of Wagner's most difficult: the 1861 Paris production of Tannhäuser was a fiasco, Wagner gave up hope of completing Der Ring des Nibelungen, the 1864 Vienna production of Tristan und Isolde was abandoned after 77 rehearsals, and finally in 1866 Wagner's first wife, Minna died. Cosima Wagner was later to write: "When future generations seek refreshment in this unique work, may they spare a thought for the tears from which the smiles arose."
The premiere was given at the Königliches Hof- und National-Theater, Munich, on June 21, 1868. The production was sponsored by Ludwig II of Bavaria and the conductor was Hans von Bülow. Franz Strauss, the father of the composer Richard Strauss played the French horn at the premiere, despite his often-expressed dislike of Wagner, who was present at many of the rehearsals. Wagner's frequent interruptions and digressions made rehearsals a very long-winded affair. After one 5 hour rehearsal, Franz Strauss led a strike by the orchestra, saying that he could not play any more. Despite these problems, the premiere was a triumph, and the opera was hailed as one of Wagner's most successful works. At the end of the first performance, the audience called for Wagner, who appeared at the front of the Royal box, which he had been sharing with King Ludwig. Wagner bowed to the crowd, breaking court protocol, which dictated that only the monarch could address an audience from the box.