Microgrids often experience many power quality issues due to the intermittency of renewable energy resources integrated into the grid. Of these power quality issues, voltage sag and swell occur frequently. Voltage sag and swell affect the performance and life time of the loads and terminal equipment. In this paper Unified Power Quality Conditioner (UPQC) is proposed for compensating voltage variations and detecting islanding. For fixing issues with voltage sag/swell, interruptions, harmonics, & reactive power, UPQC is placed at the Point of Common Coupling (PCC). A new islanding detection and reconnection method is proposed in this paper. In interconnected mode, UPQC compensates for power quality disturbances, whereas in islanded mode, Distributed Generators (DG) supply active power, and UPQC will handle reactive and harmonic power compensation. The proposed system enables continuous DG connection during voltage disturbances, ensuring robust power system stability. The simulation is implemented using MATLAB/Simulink software.