A strategy to eliminate harmonics from the grid current as well as the voltage of the point of common coupling (PCC) for microgrid applications is presented in this paper. The position of the harmonics reduction unit is selected so that it can reduce the harmonics level of the grid current and PCC voltage harmonics irrespective of the distribution of the renewable energy sources in microgrid. The proposed compensation system can be operated in both grid connected and islanded microgrid system without changing any configuration. In the proposed control algorithm, the required amount of attenuation for the harmonics is determined to meet the THD requirement specified by IEEE 519. Fast and efficient algorithm for phase detection irrespective of the presence of harmonics has been utilized for the system. The effectiveness of proposed method is verified by simulation and experimental results.