You can read a short history of the piece here.
The Medici Pope Leo X first mandated the silent recitation of Psalm 51 at the conclusion of Tenebrae. Each Catholic soul across the world considers, then, David's desperate poetic plea for God's mercy. The Pope's own singers, however, quickly adopted the practice of a fully polyphonic performance of the Psalm (later in the fifteenth century, Palestrina contributed a setting). Allegri joined the papal choir in 1629, serving for the rest of his life. In the 1630s he composed a setting of Miserere mei for Holy Week that eventually became his greatest musical legacy. The papal choir sang it every year from the seventeenth century until the choir collapsed in 1870. The penalty for copying its music, which the papal choir considered its exclusive property, was excommunication.This is the piece which Mozart copied from memory, after hearing only once.