Renewable energy sources are environmentally friendly and cost-efficient. However, the problem with these renewable resources is their heavy reliance on weather conditions. Thus, at times, these solutions are not guaranteed to meet the required demand all the time. For this, hybrid microgrids are introduced, which have a combination of both renewable energy sources and non-renewable energy resources. In this paper, a cost-efficient optimization algorithm is proposed that minimizes the use of non-renewable energy sources. It maximizes the use of renewable energy resources by meeting the demand for utility grids. Real data based on the load and demand of the utility grids in Italy is used, and a system that determines the optimal sizing of the microgrid and a daily plan is introduced to optimize the renewable resources operations. As part of the proposal, the objective function for the operation and planning of the microgrid in such a way to minimize cost is formulated. Moreover, a variant of the PSO algorithm named recurrent PSO is implemented. The recurrent PSO algorithm solves the proposed optimization objective function by minimizing the cost for the installation and working of the microgrid. Afterwards, the energy management system algorithm lays out a plan for the daily operation of the microgrid. The performance of the system is evaluated using different state-of-the-art optimization methods. The proposed work can help minimize the use of diesel generators, which not only saves financial resources but also contributes toward a green environment.