Erdogan was presiding over a Council of Ministers meeting when the man fired the shots at a security check point just past the visitors' entrance to the building, the state-run Anadolu Agency and other media reported. The suspect was quickly subdued and was being questioned by police.
It was not clear how the shooter 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.
But Anadolu, citing unnamed sources at the prime minister's office, suggested the incident was not politically-motivated.
It identified the suspect as 34-year-old Nuri Baskapan, said he had frequently written to government officials about "personal problems" and had attempted "similar acts" in the past to try and draw attention to his grief.
The private Dogan news agency said Baskapan was made redundant from a municipality job and was seeking assistance from government officials concerning a dispute over his compensation payment.
Erdogan's spokesman and security officials at the prime minister's office were not available for comment.
Erdogan says Ocalan's outlawed Kurdistan Workers' Party, or PKK, is orchestrating the hunger strike.