A Reuters witness said the shallow quake shook buildings in the capital, Taipei. The quake near the northeastern county of Yilan had a depth of 22 km (13 miles).
There were no immediate reports of significant damage or casualties.
The earthquake triggered limited power cuts and forced the suspension of regional railway service in northern Taiwan. There were also reports of gas and water leaks.
Taiwan, a self-ruled island that China considers its own, lies near the junction of two tectonic plates and is prone to earthquakes.
More than 100 people were killed in a quake in southern Taiwan in 2016, and some Taiwanese remain scarred by a 7.6 magnitude quake that killed more than 2,000 people in 1999.