The caves in the city provided refuge to King Valagamba (also called Vattagamini Abhaya) in his 14-year-long exile from the Anuradapura kingdom. Buddhist monks meditating in the caves of Dambulla at that time provided the exiled king protection from his enemies. When King Valagamba returned to the throne at Anuradapura kingdom in the 1st century BC, he had a magnificent rock temple built at Dambulla in gratitude to the monks in Dambulla.