JAKARTA, Indonesia—A strong earthquake shook Indonesia’s densely populated Java island late Friday, killing at least two people and severely damaging dozens of homes near the epicenter.

The magnitude-6.5 quake struck at 11:47 p.m. local time and was felt across the island, including about 124 miles away from the epicenter in the capital, Jakarta, where office towers and apartment buildings swayed.

A...