Mr. Erdogan was presiding over a Council of Ministers meeting when the man fired the shots just outside the visitors entrance to the building, the state-run Anadolu Agency and other media reported. The suspect, who was not identified, was quickly subdued and was being questioned by police.
It was not clear how the man managed to enter the heavily protected complex with the gun.
The incident comes amid a surge of attacks by Kurdish rebels who are fighting for self-rule for minority Kurds in southeast Turkey, as well as an increase in tensions with neighboring Syria.
Mr. Erdogan’s spokesman and security officials at the prime minister’s office were not immediately available for comment.
Private NTV television reported that ministers continued with their meeting.
More than 600 Kurdish inmates are holding a hunger strike to demand increased rights for Kurds as well as improved jail conditions for Kurdish rebel leader Abdullah Ocalan, who is serving a life sentence on a prison island near Istanbul.
Mr. Erdogan says Ocalan’s outlawed Kurdistan Workers’ Party, or PKK, is orchestrating the hunger strike.