Residents felt the quake about 190 miles away (305 kilometers) in the capital of Jakarta, where people briefly evacuated to the ground floor of their high-rises.
Tremors were also felt in the cities of Bandung, more than 63.5 miles away (102.1 kilometers) and Yogyakarta, more than 211 miles away (339.9 kilometers), authorities said.
The two victims were buried in rubble, one in Ciamis, in West Java, and the other in Pekalongan, in Central Java, emergency officials said.
A few hundred homes and buildings were destroyed or damaged, the officials said. Several hospitals were damaged. Seventy patients from Banyumas Hospital were moved to tents and another temporary shelter.
Fears of a tsunami prompted people to evacuate their homes for higher ground, but there were no reports of tsunamis occurring along the southern coasts of western, central and eastern Java and the city of Yogyakarta.
The alert was lifted at 2:30 a.m., the spokesman said.
Most residents returned to their homes on Saturday and they were advised to seek temporary shelter if their dwellings aren't safe. Several aftershocks continue to be felt in the areas hit by the quake, emergency officials said.
An earthquake on December 7, 2016, struck Indonesia's Aceh province in Sumatra and killed at least 100 people.
More about: #Indonesia