Once the Buddha was residing at Jetavana Monastery in Savatthi for the Rain’s retreat. Five hundred monks approached the Buddha and requested for a suitable object for meditation.
Having obtained a suitable object of meditation, the monks withdrew to a distant forest, near Mt Himavanta for the rainy season. They took shelter under huge trees as temporary residence and began engaging themselves intensively in the practice of meditation.
By the virtuous power of the monks, the tree-deities were unable to live at the top of the trees. Realizing the monks would continuously engage themselves in the practice of meditation for a few months before returning to their monasteries and yearning to return to their dwellings above the trees, the devas tried to frighten the monks by appearing in fearful forms and making frightful sounds. Unable to bear tit anymore, the monks decided that the place was not suitable for the practice of meditation.
They returned to the Buddha and informed Him of their difficulties and requested for another place for meditation. After contemplating on the matter, the Buddha taught them the Karaniyametta sutta and instructed them to return to the same forest and radiate loving kindness to all beings.
Having now understood the noble quest of the monks, the tree spirits took a change in their attitude. From then on, the monks meditated peacefully without any further disturbances. They also received the affectionate care and respect from the tree-spirits.
Finally, all the monks realized perfect emancipation and become the Worthy Ones (Arahants).
[ The Karaniyamettasutta starts with the mentioning of qualities that should be possessed by one who is skilled in his own welfare so that he could realize the state of peace (ie Nibbana). Then happiness is wished for all beings describing them in detail. The list covers every sentient being. Next there is an exhortation as to how loving kindness should be extended to all beings. Such practice is called the Brahma-living. The sutta is concluded by saying that if one does not resort to false views, is virtuous, is possessed of insight, he would discipline himself with regard to greed in sensual pleasures and will never come (back) to sleeping in a womb (ie he will not be reborn)]
The discourse is found in the Khuddaka-patha and Sutta-nipata and it is normally used to radiate loving kindness towards all beings so that they are well and happy.