The intermittent characteristics of microgrids (MGs) have motivated the development of energy management systems (EMSs) in order to optimize the use of distributed energy resources. In current studies, the implementation of an EMS followed by experimental-based analyses for both grid-tied and stand-alone MG operation modes is often neglected. Additionally, the design of a management strategy that is capable of preserving the storage device lifetime in battery-based MGs using a power gradient approach is hardly seen in the literature. In this context, this work presents the application of an EMS for battery-based MGs which is suitable for both grid-tied and stand-alone operation modes. The proposed EMS is formulated as an optimal power flow (OPF) problem using the-constraint method which is responsible for computing the current references used by the EMS to control the MG sources. In the optimization problem, the total generation cost is minimized such that the active power losses are kept within pre-established boundaries, and a battery management strategy based on power gradient limitation is included. Finally, the effectiveness of the proposed EMS is evaluated by two scenarios which enable detailed analyses and validation. The first considers a dispatchable and a non-dispatchable source, whereas the second a dispatchable source and a storage device. The experimental results showed that the proposed EMS is efficient in both operation modes and is also capable of smoothing the state of charge (SoC) behavior of the storage device. INDEX TERMS Battery power gradient, distributed generation, energy management system, microgrid, optimal power flow, storage device.