The quake struck at a depth of 91 kilometres (56 miles) just outside the coastal town of Cipatujah on Java island late Friday, the US Geological Survey said.
The tremor was felt across the island including in the capital Jakarta, some 300 kilometres (190 miles) from the epicentre.
A 62-year-old man in Ciamis and an 80-year-old woman in Pekalongan city were killed when the buildings they were in collapsed, national disaster mitigation agency spokesman Sutopo Purwo Nugroho said, while a 34-year-old woman from the city of Yogyakarta died when she fell while running out of her house.
"Several hospitals in several areas have also been damaged and patients had to be evacuated," Nugroho said.
A hospital in Banyumas suffered damage to ceilings, cracked walls and leaks to oxygen pipelines, forcing around 70 patients to be moved to temporary shelters.
At least nine hundred houses were also damaged in addition to schools and government offices across West Java and Central Java provinces, the national disaster mitigation agency said.
Jakarta resident Web Warouw, 50, was on the 18th floor of a building in the capital when the quake struck just before midnight local time (1700 GMT).
"Suddenly, we felt dizzy... We then realised it was a quake and immediately ran downstairs," Warouw told AFP.
Aid is being distributed and temporary tents have been set up for those who have lost their homes.
The agency was still evaluating the impact of the disaster and urged people to be cautious and stay alert.
Indonesia experiences frequent seismic and volcanic activity due to its position on the Pacific "Ring of Fire", where tectonic plates collide.
Some 170,000 lives were lost when a 9.1-magnitude quake and tsunami struck Aceh province in December 2004, which also hit coastal areas as far away as Somalia.
An earthquake struck Aceh again in December 2016, killing more than 100 people, injuring many others and leaving tens of thousands homeless.
(Except for the headline, this story has not been edited by NDTV staff and is published from a syndicated feed.)