In the current study, the penalty immersed boundary method (PIBM) is used to numerically analyze the flapping motion of an inverted flag, placed behind a bluff body using two-dimensional viscous flow. Direct numerical simulations (DNS) are carried out by changing the Reynolds number, bending stiffness, and streamwise gap (which represents the distance from the bluff body to the fixed end of the flag) behind the inverted D-shape cylinder to find their impact on the peak-to-peak amplitude, flapping frequency of the flag. The optimal values of the stated parameters are determined and explained in how the change in geometric and flow parameters affects the flapping behavior which has an ultimate impact on energy output. The addition of a bluff body caused the high strain due to higher bending modes and curvature in the flag. It is also shown that the alternating vortical flow structures have a strong influence on the dynamical behavior of the inverted flag placed inside the wake region of an inverted D-shape cylinder. Relationships between flow velocity, streamwise gap, and the flag's bending rigidity highlight consistent trends for the enhancement of energy production. Additionally, the lateral position of an inverted flag and its effect on vorticity and energy harvesting is discussed in detail with their power spectra to find the maximum amplitude and corresponding drag force. This research would help in concluding the optimal parameters of the inverted flag for the highest energy generation behind the bluff body.