You may notice that B is an intermediate to eventually reach D. Indeed, it is quite possible, performing the reaction in the lab, B would spontaneously yield D with only little chance to isolate this intermediate as such.
$\ce{NaBH4}$ reduces the aldehyde to an alcohol. Then, mediated by the acid, the actetal is converted into an aldehyde to yield B.
The aldehyde in B already is more reactive than a ketone would be, but the presence of acid (still present from converting the acetal) may increase the reactivity of the carbonyl group even further. The subsequent cyclization is favored
because the chance that alcohol and aldehyde are close enough to form a new bond is much higher for an intramolecular reaction, than for an intermolecular one. This is a general rule worth to retain. In addition, the formation of five- and six-membered rings equally is energetically favorable.