I have that happen sometimes but I don’t know if it is related to fasting. It’s usually a song line that gets stuck on repeat and even wakes me up! I’ve learned to sing a simple song in my head really really slowly (like row row row your boat) and it goes away. Good luck! I know it’s annoying.
Other than “earworms,” which is common for people to experience at any time (i.e. getting a song stuck in your head), this doesn’t sound like it’s related to fasting in the traditional sense. Could a change in blood sugar or other metabolic mechanisms during the fasting state be causing this? Maybe, but I don’t think anyone on this sub is qualified to make that judgement call.
You should probably discuss it with your doc/therapist if it’s causing “borderline panic attacks.”