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.