The Pacific Tsunami Warning Centre in Hawaii issued a tsunami warning for South America’s Pacific coast but later cancelled it.
News station Canal N said a church collapsed in Ica, south of Lima, killing at least 12 people and injuring 70.
TvPeru said others were killed or injured by collapsing buildings in the coastal cities located near the epicentre of the quake.
Witnesses said the tremors shook buildings in two bouts, lasting between 20 seconds and two minutes each.
Milagros Meneses, 35, a resident in the city of Canete south of the capital Lima, said: “I was with my children when the movement started and then the walls collapsed. My house was destroyed. The hospital gave me a tent for my kids to sleep in.”
Alan Garcia, Peru’s president, sent condolences to the families of the earthquake’s victims and said the country, which has suffered devastating earthquakes in the past, narrowly escaped a major disaster.
Garcia said: “It fortunately did not cause a catastrophe with an immense number of victims.”
There were fears that the strong temblor would have been deadly for shanty towns surrounding the capital.
The earthquake cut power lines and disrupted water services in Lima.
Local media reported a fire in one district in the capital and homes destroyed in Pisco, a poor township near the epicentre.
The health ministry declared a disaster as ambulances and rescue workers rushed to the city.
“People here hugging and crying in fear on the streets,” said Cristyane Marusiak, a resident.
The US Geological Survey said the quake hit about 40km northwest of Chincha Alta.