TEHRAN — A man wielding a machete was shot on Monday while trying to enter the central Tehran offices of Iran’s president, Hassan Rouhani, in a surprising breach of security.

The intruder, described by state-run news media as around 35 years old, wore a white martyr’s shroud, a symbol in Shiite Islam signifying willingness to die for a cause. He brushed past a lone guard at the entrance of the presidential complex, a white, two-story building in central Tehran. He then attacked a second guard, who fired once into the air before shooting the assailant in the leg, the government news agency IRNA reported. The man was hospitalized.

Mr. Rouhani was never in danger: He had left the building to attend a symposium on public transportation, local news media reported.

The incident was unusual for Iran, where security is high, especially around the most important government buildings. Security was heightened even further in June, after two militants linked to the Islamic State simultaneously attacked the Parliament and the tomb of the founder of the Islamic republic, Ayatollah Ruhollah Khomeini, killing 17 and wounding dozens.

Nerves have been put on edge lately by nationwide protests that started on Dec. 28 and raged in around 80 cities amid widespread criticism of Mr. Rouhani’s economic and social policies. There have also been protests recently by individual women against the compulsory wearing of Islamic head scarves.

The governor-general of Tehran, Mohammad Hossein Moghimi, said the motives of Monday’s attacker were not clear. “We should wait for his condition to improve and the interrogation process to start so that the details of the affair will become known,” he told the semiofficial Fars news agency.

The entrance to the presidential complex is open to the public, and has been the site of anti-government demonstrations. Many people also go there to deliver letters to government representatives or, perhaps, to speak with them.