There are two main legends associated with Karni Mata Temple,
Legend has it that Laxman, Karni Mata's stepson (or the son of one of her storytellers), drowned in a pond in Kapil Sarovar in Kolayat Tehsil while he was attempting to drink from it. Karni Mata implored Yama, the god of death, to revive him. First refusing, Yama eventually relented, permitting Laxman and all of Karni mata's male children to be reincarnated as rats.
Local folklores say that 20,000 strong army men deserted a nearby battle and came running to deshnoke. Upon learning the sin of desertion, punishable by death, Karni Mata spared the lives but made them rats and offered the temple as the future place of stay. The army of soldiers expressed their gratitude and promised to serve Karni Mata lifelong.
It's a strong belief that If one of the rats is killed, it must be replaced with one made of solid gold. Eating food that has been nibbled on by the rats is considered to be a "high honor"
Karni Mata Temple|Wiki