We present a detailed analysis of the beam energy dependence of the mechanisms for the generation of directed and elliptic flows in Au+Au collisions focusing on the role of hadronic rescattering and spectator shadowing within a microscopic transport model JAM with different equation of state. A systematic study of the beam energy dependence is performed for Au+Au collisions at √ sNN = 2.3 − 62.4 GeV. The transition of the dynamical origin of the directed flow is observed. We find that the initial Glauber type nucleon-nucleon collisions generate negative v1 for nucleons at midrapidity due to the presence of spectator matter, and this negative nucleon v1 is turned to be positive by the meson-baryon interactions at the beam energy region of √ sNN < 30 GeV. In contrast, above 30 GeV there is no spectator shadowing at midrapidity, and initial nucleon-nucleon collisions do not generate directed flow, but subsequent rescatterings among produced particles generate negative v1 for nucleons. It is demonstrated that negative pion-directed flows are mostly generated by the interaction with the spectator matter. It is also shown that the squeeze-out effect is largely suppressed in the case of softening, which leads to the enhancement of elliptic flow around √ sNN = 5 − 7 GeV. The elliptic flow at midrapidity above 10 GeV is not influenced by the squeezeout due to spectator matter, while its effect is seen at the forward rapidity range of y/yc.m. > 0.5, which decreases as beam energy increases.