The quake killed 17 people in Sichuan and five in the neighboring province of Yunnan, the agency said. About 100 people in Sichuan and 35 in Yunnan were injured, Xinhua said.
Residents of Kunming, the Yunnan's capital, felt a strong shock from the earthquake, with many running into the streets despite falling rain, the China News Service said.
Telecommunications were temporarily cut but then restored, it said.
A duty officer at the State Seismology Bureau said the quake occurred in a mountainous area. He did not want to give his name, as is common with officials in China.
The bureau is still gathering information, he said.
On May 12, a 7.9 magnitude earthquake in northern Sichuan killed nearly 70,000 people and left 5 million homeless. The region has been hit by scores of aftershocks, keeping people there on edge.