Ordinary Lutheran worship in Bach's Leipzig needed only two movements of the Mass, the Kyrie and the Gloria, the opening pair a Lutheran service actually retained from the older Catholic rite. A "Missa tota," the full Kyrie-Gloria-Credo-Sanctus-Agnus Dei sequence, was Catholic liturgical furniture, not Lutheran, and had no obvious use in any church Bach served across his career. That did not stop him from assembling exactly that, in full, across the final years of his life.
The pieces did not start life as one project. The Kyrie and Gloria date to 1733, composed during five months of official mourning after the death of the Saxon elector and presented to his successor, Augustus III, as an audition piece for a court title Bach badly wanted. The Sanctus reworks a piece written for Christmas services back in 1724. Only in 1748 and 1749, in the last two years of his life, did Bach return to the project and compose the remaining Credo movements fresh, adding "Et incarnatus est" last of all, a movement Christoph Wolff and other scholars regard as likely the final significant piece of music Bach ever wrote.
Why he bothered, given no Lutheran service he led would ever need it, is genuinely unsettled among scholars. One proposal ties the finished Mass to the dedication of Dresden's new Catholic Hofkirche, under construction since 1738; another reads it as Bach's own attempt, near the end of his career, to leave behind a summation of everything he knew about sacred vocal writing, addressed to no particular liturgy at all.