Brandishing a pistol, the attacker fired several times before being detained by police, an AFP reporter said. Dundar, the editor-in-chief of leading Turkish opposition daily Cumhuriyet, was unharmed.
Dundar was outside the courthouse during a break as the court prepared to deliver its verdict in his trial for allegedly revealing state secrets in a newspaper story.
The assailant fired twice or three times in front of the cameras of media who had crowded outside Istanbul's main courthouse during the closed door trial.
"I am okay ... The court was in a break to deliver a verdict. The attack occurred after we went out to wait for the ruling," Dundar said.
More From This Section
"I don't know who or what it is. I only saw the gun had been pointed at me," he added.
Dundar and Erdem Gul, his Ankara bureau chief, are charged with revealing state secrets over a story accusing the government of seeking to illicitly deliver arms bound for Syria.
Cumhuriyet's report on a shipment of arms intercepted at the Syrian border in January 2014 sparked a furore when it was published last May, fuelling speculation about Turkey's role in the Syrian conflict and its alleged ties to Islamist groups in the country.
Accusing Erdogan and pro-government media of whipping up a climate of hatred against him, Dundar said: "We know very well who showed me as a target.