To break it down:

Our earliest possibly reliable (not chronologically the earliest) source describing something close to the story is a reference in article that was published in an Italian journal called La Settimana del Clero in 1947 by a Fr. Domenico Pechenino, who worked at the Vatican during the time of Leo XIII. It’s essentially the link that Gary gave: Leo XIII was “attending a Mass of thanksgiving” (the year was not specified) when he seemed to be staring at “something grave and unusual.” He retired to his private office, with his attendants asking if he was well. Half an hour later he had written the St Michael prayer and “requested that it be printed and sent to all the ordinaries around the world.” What exactly Leo saw was not specified, only that it was something that startled the pope.

I do not remember the exact year. One morning the great Pope Leo XIII had celebrated a Mass and, as usual, was attending a Mass of thanksgiving. Suddenly, we saw him raise his head and stare at something above the celebrant’s head. He was staring motionlessly, without batting an eye. His expression was one of horror and awe; the colour and look on his face changing rapidly. Something unusual and grave was happening in him.

Finally, as though coming to his senses, he lightly but firmly tapped his hand and rose to his feet. He headed for his private office. His retinue followed anxiously and solicitously, whispering: ‘Holy Father, are you not feeling well? Do you need anything?’ He answered: ‘Nothing, nothing.’ About half an hour later, he called for the Secretary of the Congregation of Rites and, handing him a sheet of paper, requested that it be printed and sent to all the ordinaries around the world. What was that paper? It was the prayer that we recite with the people at the end of every Mass. It is the plea to Mary and the passionate request to the Prince of the heavenly host, (St. Michael: Saint Michael the Archangel, defend us in battle) beseeching God to send Satan back to hell.

The second account comes from Cardinal Giovanni Nasalli Rocca di Corneliano (1872-1952), who wrote in his Litteris Pastoralibus pro Quadragesima (Pastoral Letters for Lent) for the Diocese of Bologna. In this version, Leo is said to have seen a vision of demons in Rome, which led him to write the prayer, but here it is never said as to when he saw it:

Leo XIII himself wrote that prayer. The sentence ‘The evil spirits who wander through the world for the ruin of souls’ has a historical explanation that was many times repeated by his private secretary, Monsignor Rinaldo Angeli. Leo XIII truly saw, in a vision, demonic spirits who were congregating on the Eternal City (Rome). The prayer that he asked all the Church to recite was the fruit of that experience. He would recite that prayer with strong, powerful voice: we heard it many a time in the Vatican Basilica. Leo XIII also personally wrote an exorcism that is included in the Roman Ritual. He recommended that bishops and priests read these exorcisms often in their dioceses and parishes. He himself would recite them often throughout the day.

So essentially we have two stories for the cause of the development of the prayer here: