Yemen's embattled president survived an apparent attempt to kill him on Friday as fighting intensified in Sana'a amid warnings that the country is sliding inexorably into all-out war.

Ali Abdullah Saleh, who has ruled Yemen for 32 years, emerged lightly injured but defiant from his compound in the capital after shells or a missile hit a mosque inside, killing three guards and a cleric and injuring two other senior government figures.

The incident fuelled growing anxieties that the Arab uprisings, which have brought dramatic changes to Egypt and Tunisia, are turning Yemen – the Arab world's poorest country – into something far more volatile and dangerous.

The government accused Saleh's bitter rival, Hamid al-Ahmar of the opposition Islah party, of launching the attack, and government forces immediately shelled his mansion in Sana'a.

"The al-Ahmars have committed a great crime, and crossed a red line," said Tareq al-Shami, a government spokesman. "This was an attempted coup, and the government will take all necessary measures."

Sadeq al-Ahmar, head of the powerful Hashid tribal federation, blamed Saleh himself for the shelling, saying it was carried out to help justify the government's escalation of streetfighting in the capital.

Saleh, who was treated at a military hospital, was reported to be preparing to give a press conference later. An opposition TV station initially reported him dead, which made headlines around the world.

Abdul Ghani al-Iryani, a Yemeni political analyst, told al-Jazeera TV it was "quite reasonable to assume" that Ahmar's fighters were behind the palace attack. "[The tribesmen] probably wanted him to know that [Saleh] can no longer attack them with impunity, and that they can reach him as he can reach them," Iryani said.

Other regional analysts say the chances for a democratic or peaceful transition of power in Yemen are slim.

It was a violent end to a violent week. On Thursday government jets strafed roads and villages north of Sana'a as thousands of tribesmen tried to enter the capital to fight Saleh loyalists. Residents described an atmosphere of fear and alarm at food shortages and rising prices.

Saleh has reneged on a deal brokered by Saudi Arabia and other Gulf states under which he would step down in return for an amnesty and free elections.

The US, which has leaned on Saleh in its fight against al-Qaida, has increased pressure for him to go, blaming the bloodshed on his backpedalling from the agreement. Britain and the EU are also pushing hard for him to implement the deal.

The latest violence is likely to persuade Saudi Arabia, which has strong ties with the Yemeni tribes, to press Saleh to step aside, to avert disaster in a country where half the population owns a gun.

The attack on the president came after Friday prayers, with heavy artillery repeatedly striking the presidential compound and shaking nearby buildings.

The streets were deserted after many residents fled the city for the safety of nearby villages.

"People there were happy to hear that he [the president] had been killed, but then the government denied that," said Ibrahim Mothana, 22, a student. "It means we will experience an escalation and it will probably be more violent."

Another resident told the Guardian: "The streets are empty. I managed to go out yesterday to buy food but prices are going up daily. In the street where I live all the families have gone back to their villages and just left a family member to stay behind to look after the house. There must be more than 100,000 in total who have left the city."

The foreign secretary, William Hague, said: "British Nationals in Yemen should leave immediately while commercial flights are still operating. Given that we cannot expect forewarning of any airport closures, British nationals should not wait to leave. We have already said that in such a difficult security situation it is extremely unlikely the British government will be able to evacuate British nationals. In light of the latest escalations, I cannot restate this strongly enough. You should not plan for, nor expect, the British government to be in a position to assist you to reach safety."

The European Union high representative for foreign affairs, Catherine Ashton, deplored the loss of life and said the EU would help EU citizens wishing to leave Yemen. "I have repeatedly urged President Saleh to listen to the demands of the Yemeni people and transfer power," she added.

The US emphasised the need for an orderly transfer of power. "Violence cannot resolve the issues that confront Yemen, and today's events cannot be a justification for a new round of fighting," a White House spokesman said. President Barack Obama's counterterrorism adviser, John Brennan, spent two days this week trying to persuade the Saudis and the UAE to boost efforts to help end the violence.

On Thursday, the official Yemeni news agency said the government was, again, willing to endorse the Gulf-brokered transition agreement.