A woman is seen inside her house damaged after an earthquake hit Indonesia. (Reuters photo)

A powerful undersea earthquake that struck off the Indonesian islands of Sumatra and Java has killed four people and damaged hundreds of buildings, the nation's disaster agency said on Saturday.

Indonesia issued a tsunami warning within minutes of the 6.8-magnitude quake on Friday night. The warning lasted two hours and sent people living along the coastline fleeing to higher ground.

The strong quake was felt in the capital Jakarta and people rushed out of high-rise buildings.

Four people died, some of heart attack or fatigue, as people rushed away from the coastline, Agus Wibowo, spokesman of the disaster mitigation agency told a news briefing. Four others were injured, and more than 200 buildings were damaged by the quake, he said.

Many of the people who took to higher ground, including about 1,000 taking shelter in the gubernatorial office in the Lampung province, had returned home, Wibowo said.

The U.S Geological Survey put the epicentre in the Indian Ocean about 227 km (141 miles) from the city of Teluk Betung on Sumatra.

This lies along the Sunda fault, where researchers have long found a potential for a megathrust quake, Wibowo said, referring to a very large earthquake that occurs in a region where one of the earth's tectonic plates is thrust under another.

Wibowo warned that a study by Indonesia's geophysics agency showed there is a potential quake of 8.8 magnitude in the area that would trigger a 20-metre high tsunami, though he added that no one can predict when it would happen.

"People have to be alert, houses need structures that can withstand quakes," he said. His agency has also launched projects to educate people living along the coastline on how to respond to a tsunami threat, he said.

Indonesia is situated on the so-called Pacific Ring of Fire, which is frequently hit by earthquakes that are sometimes accompanied by tsunamis.

The most devastating in recent Indonesian history was on Dec. 26 in 2004, when a magnitude 9.5 quake, which scientists have associated with a megathrust event, triggered a massive tsunami that killed around 226,000 people along the shorelines of the Indian Ocean, including more than 126,000 in Indonesia.

Last year, a tsunami hit the city of Palu in Sulawesi island, killing thousands, while a crater collapse at the Anak Krakatau volcano triggered a tsunami that killed at least 430 people in an area near the latest quake.