In practical applications, the photovoltaic and energy storage (PV&ES) system can reduce grid-connected power fluctuations and help solve the problems caused by the large amount of distributed photovoltaic generation connected to the grid. However, the presence of non-ideal factors such as rich voltage harmonics and large grid impedance in the grid may lead to the destabilization of the PV&ES system, so analyzing the stability of the PV&ES system under complex grid is needed. The emphasis of this paper is on the modeling and stability analysis of the PV&ES system. The accurate model is established by using the harmonic linearization, and stability analysis under different grid impedances is carried out using impedance analysis. As proved, the PV&ES system has better stability under weak grid, but poor stability under strong grid with lower phase margin. Based on this, a virtual resistance-based method for improving the PV&ES system output impedance characteristics is proposed to achieve better stability under different grid impedance conditions. The validity and feasibility of the above analysis are confirmed through simulation of three-phase grid-connected inverters prototype.