In this paper, a nonlinear adaptive backstepping controller is designed to control the bidirectional power flow (charging/ discharging) of battery energy storage systems (BESSs) in a DC microgrid under different operating conditions. The controller is designed in such a manner that the BESSs can store the excess energy from the renewable energy sources (RESs) in a DC microgrid after satisfying the load demand and also feeding back the stored energy to the load when RESs are not sufficient. The proposed controller is also designed to maintain a constant voltage at the DC bus, where all components of DC microgrids are connected, while controlling the power flow of BESSs. This paper considers solar photovoltaic (PV) systems as the RES whereas a diesel generator equipped with a rectifier is used as a backup supply to maintain the continuity of power supply in the case of emergency situations. The controller is designed recursively based on the Lyapunov control theory where all parameters within the model of BESSs are assumed to be unknown. These unknown parameters are then estimated through the adaptation laws and whose stability is ensured by formulating suitable control Lyapunov functions (CLFs) at different stages of the design process. Moreover, a scheme is also presented to monitor the state of charge (SOC) of the BESS. Finally, the performance of the proposed controller is verified on a test DC microgrid under various operating conditions. The proposed controller ensures the DC bus voltage regulation within the acceptable limits under different operating conditions.