The integration of hybrid renewable energy sources (HRESs) into the grid is currently being encouraged to meet the increasing demand for electric power and reduce fossil fuels which are causing environmental-related problems. Integration of HRESs into the grid can create some power quality (PQ) problems. To mitigate PQ problems and improve the performance of grid-connected HRESs some flexible devices should be used. This paper presents a distributed power flow controller (DPFC), as a type of flexible device to mitigate some PQ problems, including voltage sag, swell, disruptions, and eliminating the harmonics in a hybrid power system (HPS). The HPS presented in this work comprises a photo voltaic (PV) system, wind turbine (WT) and battery energy storage system (BESS). As a result, black widow optimization (BWO) with DPFC with real and reactive power (DPFC-PQ) is built in this paper to solve the PQ issues in HRES systems. The main aim of the work is to mitigate PQ problems and compensate for load demand in the HRES scheme. The controller used to drive this DPFC-PQ is a fractional-order PID (FOPID) controller optimized by the black widow optimization (BWO) technique. To assess the capability of BWO in fine-tuning the FOPID controller parameters, twelve optimization techniques were presented: P&O, PSO, Cuckoo, GA, GSA, BBO, Whale, ESA, RFA, ASO, and EVORFA. Additionally, a comparison between the FOPID controller and the classical PI controller is introduced. The results showed that the proposed BWO-FOPID controller for DFPC had mitigated the PQ problems in grid-connected HRESs. The system’s performance with the presented BWO-FOPID controller is compared with eleven optimization techniques used to optimize the FOPID controller and also compared with the conventional PI controller. The design of the proposed system is implemented in the MATLAB/Simulink platform and performances were analyzed.