Microgrids (MGs) are presented as a cornerstone of smart grid, which can integrate renewable energy sources environmentally, friendly, and reliably. Hierarchical control strategies, i.e. primary droop control, second automatic generation control (AGC), and tertiary economic dispatch (ED), are widely used to control and optimise an MG. However, the gap between large time-scale ED and small time-scale AGC may decrease the economical efficiency of an MG. To address this problem, this study proposes a distributed economic AGC (EAGC) algorithm. The algorithm is fully distributed, as each distributed generator is assigned with an agent and the agents only require the local measurements and communication with its neighbours. Compared with centralised algorithms, the distributed control algorithm is easy to fulfil the plug-and-play requirements and reduces costs to modify the architecture of an MG. Furthermore, the proposed EAGC algorithm is suitable for all possible operation modes of an MG, i.e. islanded mode, grid-connected mode, and transition mode. To improve the robustness against agent loss or communication link fault, the topology of agents' communication network is designed to satisfy the N − 1 rule. The effectiveness of the proposed algorithm is demonstrated through the simulation tests which involve plenty of case studies.