For 5G base stations equipped with multiple energy sources, such as energy storage systems (ESSs) and photovoltaic (PV) power generation, energy management is crucial, directly influencing the operational cost. Hence, aiming at increasing the utilization rate of PV power generation and improving the lifetime of the battery, thereby reducing the operating cost of the base station, a hierarchical energy management strategy based on the improved dung beetle optimization (IDBO) algorithm is proposed in this paper. The first control layer provides bus voltage control to each power module. In the second control layer, a dynamic balance control strategy calculates the power of the ESSs using the proportional–integral (PI) controller and distributes power based on the state of charge (SOC) and virtual resistance. The third control layer uses the IDBO algorithm to solve the DC microgrid’s optimization model in order to achieve the minimum daily operational cost goal. Simulation results demonstrate that the proposed IDBO algorithm reduces the daily cost in both scenarios by about 14.64% and 9.49% compared to the baseline method. Finally, the feasibility and effectiveness of the proposed hierarchical energy management strategy are verified through experimental results.