An online energy management strategy for a residential Microgrid (MG) based on battery energy storage systems (BESS) and diesel distributed generators (DGs) is proposed. The economic dispatch problem is solved online using a centralized dynamic algorithm and the distributed real-time local measurements of injected power of each DG. The dynamic algorithm minimizes the total energy cost in the MG while the load-generation balance is guaranteed without exceeding the maximum power generation capacities. The optimal active power references are tracked by each DG using feedback linearizing inner controllers, which guarantees a stable operation of the inner dynamics. The convergence and optimality properties of the energy management strategy are proved. The BEES based DG dynamic model is developed, and the feedback linearizing inner controller design is presented. The effectiveness and compatibility of the energy management strategy and the inner controller design are validated in an electromagnetic transient simulation performed in Matlab/Simulink-Simpower.