A suicide bombing outside one of Islam's holiest sites was carried out by a Pakistani man who moved to the kingdom 12 years ago to work as a driver, Saudi Arabia officials have said.
Four Saudi security officers were killed and five others injured in Monday's attack in a car park outside the sprawling mosque grounds where the Prophet Muhammad is buried in Medina.
An Interior Ministry statement issued on Tuesday identified the bomber as 34-year-old Abdullah Qalzar Khan. It said he lived in the Red Sea port city of Jeddah with "his wife and her parents". It gave no other details.
No group has yet claimed responsibility for the attack, or others near the US Consulate in Jeddah and a suicide car bombing near a Shiite mosque in eastern Saudi Arabia, which raised fears of a co-ordinated assault aimed at destabilising the Western-allied kingdom.
Millions of Muslims from around the world visit the mosque in Medina every year as part of their pilgrimage to Mecca.
The interior ministry said the attacker set off the bomb after security officers raised suspicions about him.
Several cars caught fire and thick plumes of black smoke were seen rising from the site of the explosion as thousands of worshippers crowded the streets around the mosque.
Altayeb Osama, a 25-year old Sudanese visitor to Medina and resident of Abu Dhabi, said he heard two large booms about a minute apart as he was heading towards the mosque for sunset prayers.
He said police and fire engines were on the scene within seconds.
"It was very shocking that such a thing happens in such a holy place for Muslims, the second holiest place in the world. That's not an act that represents Islam," he said.
"People never imagined that this could happen here."
The ruling Al Saud family derives enormous prestige and legitimacy from being the caretakers of the hajj pilgrimage and Islam's holiest sites in Mecca and Medina. The attack may have been an attempt to undermine the Saudi monarchy's claim of guardianship.
In 1979, extremists took over Mecca's Grand Mosque, home to the cube-shaped Kaaba, for two weeks as they demanded the royal family abdicate the throne.
Last year, the Saudi government was accused of gross negligence by regional enemies, primarily Iran, after a crush of pilgrims during the annual hajj killed at least 2,426 people and a crane collapse over the Grand Mosque killed 111 worshippers.
The Prophet Muhammad's mosque was packed with worshippers on Monday evening during the final days of the Muslim holy month of Ramadan, which ends in the kingdom on Tuesday.
State-run news channel al-Ekhbariya aired live video of the mosque filled with people praying hours after the explosion. It also showed footage of Saudi King Salman's son and the Governor of Medina, Prince Faisal bin Salman, visiting security officers wounded in the blast and the site of the explosion.
Saudi Arabia has been a target of Islamic State attacks that have killed dozens of people. In June, the interior ministry reported 26 terror attacks in the last two years.