By Agence France-Presse
The quake struck four kilometers (2.5 miles) below the surface at 2352 GMT in the town of Antiguo Cuscatlan near the capital, the environment ministry said on Twitter.
The National Civil Police said a person was killed when a rock fell on their vehicle on a highway west of the capital San Salvador.
The Red Cross said separately that two people were taken to hospital after being injured in the same area.
Civil Protection chief Jorge Melendez said some people had been evacuated from their homes due to rockslides.
"As we have had a 5.1-strength earthquake at a fairly shallow level of four kilometers we have decided to raise the level of alert to orange, meaning search and rescue operations," he said on the radio.
National radio said the quake caused traffic jams and power failures in some places. Ambulance sirens wailed around the capital as emergency services came out to assess the quake damage.
It was the strongest quake felt so far after a series of more than 100 tremors that have been felt since Sunday.
In November a 7.0 magnitude offshore quake shook El Salvador and Nicaragua but no damage or casualties were reported.
In November 2015 a 5.4-magnitude earthquake struck off the coast of El Salvador without causing any victims.
In October 2014, a powerful 7.3-magnitude quake struck off the Central American country's coast, killing one person and wrecking homes.