Microgrids are a part of the power system that consists of one or more units of distributed generation and are expected to remain in operation after being disconnected from the system. Since they rely on overlying networks, frequency control is very important for network-independent operation. Some of the most common problems in independently operating microgrids are frequency sustainability and its fluctuations. The main purpose of this study is to control the frequency of a microgrid in island mode in different scenarios. The objective function is defined based on time and changes in the system frequency. Thus, the variable parameters of the PID controller are transformed into an optimization problem and are solved through the hybrid PSO-GSA algorithm. The study considers four scenarios: (a) a microgrid dynamic model and optimal PID controller coefficients; (b) variable velocity disturbance applied to the studied system in order to observe power changes and the microgrid frequency; (c) stepped load changes applied to the studied system; and (d) the proposed methods on the standard test function. Simulations under different operating conditions are performed, indicating improvements in the stability of microgrid frequency fluctuations by means of the proposed control method.