What is the mechanism behind recurring or looping thoughts?
I think what you're talking about is worry and/or rumination, both of which describe a perseverative and repetitive thinking style (e.g., Watkins, 2008).  Worry is future-oriented whereas rumination is past-oriented. We don't understand the mechanisms all that well at the level of analysis you're interested in, but I'll present some brain-level theories/data about these processes.

Rumination

Rumination is associated with functional hyperconnectivity between the default mode network (DMN) and subgenual prefrontal cortex (sgPFC), which may be mediated by connections with the medial dorsal thalamus (Hamilton, Farmer, Fogelman, &amp; Gotlib, 2015). The DMN is involved with affective self-referential thought and the sgPFC supports affective behavioral withdrawal.  Together, the DMN and sgPFC might produce a ruminative state that is "self-focused, valenced, and withdrawn" (Hamilton et al., 2015, p. 227).  Other analyses also reveal structural and resting state abnormalities associated with rumination, involving areas related to cognitive control (Kuhn, Vanderhasselt, de Raedt, &amp; Gallinat, 2012).  Rumination and cognitive control deficits (e.g., in working memory; Joormann, Levens, &amp; Gotlib, 2011) are strongly related, explaining why trait ruminators have difficulties with disengaging from their repetitive, intrusive thoughts.

Worry

Worry seems to be associated with increased activation of anterior cingulate and dorsal medial prefrontal cortex, involved with mentalizing and self-reflection.  And pathological worry (in GAD) may be associated with prolonged activation of these areas, possibly representing difficulties inhibiting worrying thoughts (Paulesu et al., 2010).  Hyperanxious individuals also tend to show greater amygdala and insula activity to emotional stimuli (Etkin &amp; Wager, 2007).  This may indicate that pathological worry arises from dysfunctional salience processing ("Is this stimulus biologically relevant to me?") and emotional processing/reactivity.