Here's a typical rendition of the story:
Writing down ‘Miserere’ by Italian composer Gregorio Allegri, was punishable by excommunication, but 14 year old Mozart committed it to memory…
Miserere, by Italian composer Gregorio Allegri (1582-1652), is a setting of Psalm 51 composed during the reign of Pope Urban VIII, probably during the 1630s, for use in the Sistine Chapel during matins as part of the exclusive Tenebrae service on Wednesday and Friday of Holy Week. It was the last of twelve misereres composed and chanted at the service since 1514, and the most popular. The Vatican wanted to preserve the music’s reputation for mystery and inaccessibility so it was forbidden to be transcribed and was only performed at those particular services. Writing it down or performing it elsewhere was punishable by excommunication.
According to a popular story (backed up by family letters), the fourteen-year-old Mozart was visiting Rome when he first heard the piece during the Wednesday service. Later that day, he wrote it down entirely from memory, returning to the Chapel that Friday to make minor corrections. Some time during his travels, he met the British historian Dr Charles Burney, who obtained the piece from him and took it to London, where it was published in 1771. Mozart was summoned to Rome, but instead of excommunicating the boy, the Pope showered him with praise for his feat of musical genius and the ban was lifted.
So how accurate is this and why, if it's true, was Mozart not excommunicated or something?
The story of Mozart writing everything down seems to be true, because we have it straight from his father, Leopold Mozart:
You have often heard of the famous Miserere in Rome, which is so greatly prized that the performers in the chapel are forbidden on pain of ex communication to take away a single part of it, to copy it or to give it to anyone. But we have it already. Wolfgang has written it down and we would have sent it to Salzburg in this letter, if it were not necessary for us to be there to perform it. But the manner of performance contributes more to its effect than the composition itself. So we shall bring it home with us. Moreover, as it is one of the secrets of Rome, we do not wish to let it fall into other hands, ut non incurramus mediate vel immediate in censuram Ecclesiae.
The Latin bit means "not to incur directly or indirectly the censure of the Church." But Leopold also wrote a few days later that apparently, they had no need to fear anyone's wrath.
On reading the article about the Miserere, we simply burst out laughing. There is not the slightest cause for anxiety. Everywhere else far more fuss is being made about Wolfgang's feat. All Rome knows and even the Pope himself that he wrote it down. There is nothing whatever to fear; on the contrary, the achievement has done him great credit, as you will shortly hear.
The truth is that other copies of the Miserere did exist, and it had actually been performed in London a few decades before. In fact, Mozart might have been able to buy a copy, as they were made available by some publishers.
It's interesting when Leopold says
But the manner of performance contributes more to its effect than the composition itself.
Part of the beauty of the piece is in the ornamentation used, and much of this would not necessarily have been written down. These would have been common practice when the piece was written, but obviously things change, so what Mozart heard and wrote down, or what we today hear, may be very different from what Allegri heard. Many of the ornaments have been explicitly written down in later editions, making them seem like a set-in-stone part of the piece when in fact they were one particular way of singing that phrase.
The singers at the Vatican may have been specially trained in singing Renaissance pieces in ways that other choirs and singers would not have been (since the music was in use in Rome long after it had become unfashionable elsewhere). So the unmatched beauty that people attribute to the piece was probably also a product of hearing a style of singing few would encounter elsewhere.