http://www.accesstoinsight.org/tipitaka ... .piya.html
The occasion for this discourse, in brief, according to the commentary, is as follows:
The city of Vesali was afflicted by a famine, causing death, especially to the poor folk. Due to the presence of decaying corpses the evil spirits began to haunt the city; this was followed by a pestilence. Plagued by these three fears of famine, non-human beings and pestilence, the citizens sought the help of the Buddha who was then living at Rajagaha.
Followed by a large number of monks including the Venerable Ananda, his attendant disciple, the Buddha came to the city of Vesali. With the arrival of the Master, there were torrential rains which swept away the putrefying corpses. The atmosphere became purified, the city was clean.
Thereupon the Buddha delivered this Jewel Discourse to the Venerable Ananda, and gave him instructions as to how he should tour the city with the Licchavi citizens reciting the discourse as a mark of protection to the people of Vesali.
The Venerable Ananda followed the instructions, and sprinkled the sanctified water from the Buddha's own alms bowl. As a consequence the evil spirits were exorcised, the pestilence subsided. Thereafter the Venerable Ananda returned with the citizens of Vesali to the Public hall where the Buddha and his disciples had assembled awaiting his arrival. There the Buddha recited the same Jewel Discourse to the gathering...