As greenhouse gases emissions continue to rise, society is actively seeking methods to reduce them. Microgrids (MGs), which predominantly consist of renewable energy sources, play a significant role in achieving this objective. This paper proposes an optimized methodology for power dispatch in MGs using mixed-integer linear programming (MILP). The MGs include photovoltaic systems, wind turbines, biogas (BG) generators, battery energy storage systems (BESS), electric vehicles (EV), and loads. The model features an objective function focused on cost minimization, power balance, and the necessary limits and constraints for the system’s safe operation. Real-time pricing is employed for energy transactions between the MGs and the main grid. The results demonstrate a cost-efficient operation for the proposed system comprising two MGs and the main grid. During periods of negative power balance, the demand was met by discharging the BESS, EV’s battery, or purchasing energy from the grid. The BESS was charged when energy prices were low and discharged during peak demand periods and high energy prices. The intermittent nature of renewable sources necessitates an efficient management system to ensure reliable operation. Additionally, storage systems help mitigate the variability in generation. The BG generator was another crucial component for power supply due to its flexibility. Integrating these components into the system improved reliability and ensured a secure and balanced operation.