Mozart fell ill while in Prague for the premiere, on 6 September 1791, of his opera La clemenza di Tito, which was written in that same year on commission for the Emperor's coronation festivities, His health deteriorated on 20 November, at which point he became bedridden, suffering from swelling, pain, and vomiting.
The Magic Flute was the last opera Mozart composed, it was premiered on 30th September 1791 - roughly three months before he died. Mozart himself conducted the orchestra, while the librettist, Emanuel Schikaneder, sang the role of Papageno.