KABUL, Afghanistan — An Afghan police commander opened fire Friday on two Associated Press journalists, killing Pulitzer Prize-winning photographer Anja Niedringhaus and wounding veteran correspondent Kathy Gannon — the first known case of a security insider attacking journalists in Afghanistan.

The shooting was part of a surge in violence targeting foreigners in the run-up to Saturday's presidential elections, a pivotal moment in Afghanistan's troubled recent history that promises to be the nation's first democratic transfer of power.

Niedringhaus, 48, who had covered conflict zones from the Balkans in the 1990s to Iraq, Libya and Afghanistan, died instantly of her wounds.

Gannon, 60, who for many years was the news organization's Afghanistan bureau chief and currently is a special correspondent for the region, was shot three times in the wrists and shoulder. After surgery, she was in stable condition and spoke to medical personnel before being flown to Kabul.

Niedringhaus and Gannon had worked together repeatedly in Afghanistan since the 2001 U.S.-led invasion, covering the conflict from some of the most dangerous hotspots of the Taliban insurgency. They often focused on the war's impact on Afghan civilians, and they embedded several times with the Afghan police and military, reporting on the Afghan government's determination to build up its often ill-equipped forces to face the fight against militants.

Gannon, who had sources inside the Taliban leadership, was one of the few Western reporters allowed into Afghanistan during the militant group's rule in the 1990s.

FILE - In this Thursday, April 2005 file photo, Associated Press photographer Anja Niedringhaus poses for a photograph in Rome. Niedringhaus, 48, was killed and an AP reporter was wounded on Friday, April 4, 2014, in Afghanistan.

The two journalists were traveling in a convoy of election workers delivering ballots in the eastern city of Khost, under the protection of Afghan security forces. They were in their own car with a translator and an AP Television News freelancer waiting for the convoy to move after arriving at the heavily guarded security forces base in eastern Afghanistan.

A unit commander identified by authorities as Naqibullah walked up to the car, yelled "Allahu Akbar" — God is Great — and fired on them in the back seat with his AK-47, said the freelance videographer, who witnessed the attack, which left the rear door of the car riddled with bullet holes. The officer then surrendered to the other police and was arrested.

While there have been repeated cases in recent years of Afghan police or military personnel opening fire on and killing international troops working with the country's security forces, Friday's attack was the first known insider shooting of journalists.

Past attacks have been carried out by suspected Taliban infiltrators or Afghans who have come to oppose the foreign presence in the country. At their worst, in 2012, there was an average of nearly one a week, killing more than 60 coalition troops and prompting NATO to reduce joint operations with Afghan forces.

Taliban spokesman Zabihullah Mujahid denied responsibility for Friday's attack. Khost Provincial Police Chief Faizullah Ghyrat said the 25-year-old attacker confessed to the shooting and told authorities he was from Parwan province, northwest of Kabul, and was acting to avenge the deaths of family members in a NATO bombing there. The claim could not be corroborated and officials said they were still investigating the shooter's background.

Ghyrat said the police commander told authorities he had seen the journalists, decided to act, and then demanded the assault rifle from one of his subordinates.

The shooting came on the eve of Afghanistan's elections for a new president and provincial councils. With international combat forces preparing to withdraw by the end of this year, the country is so unstable that the very fact the vote is being held has been touted as one of the few successes in outgoing President Hamid Karzai's tenure.

The Taliban have vowed to disrupt the vote and have stepped up violence in recent weeks, including increased attacks on civilian targets in Kabul and the killings of a Swedish journalist and an Afghan journalist for the French news agency Agence France-Presse.

Karzai said in a statement that he "grieved" Niedringhaus' death and wished a quick recovery for Gannon. He also ordered an investigation into the shooting.

In a memo to staff, AP President Gary Pruitt remembered Niedringhaus as "spirited, intrepid and fearless, with a raucous laugh that we will always remember."

"Anja is the 32nd AP staffer to give their life in pursuit of the news since AP was founded in 1846," he wrote. "This is a profession of the brave and the passionate, those committed to the mission of bringing to the world information that is fair, accurate and important. Anja Niedringhaus met that definition in every way."

Niedringhaus joined the AP in 2002, and while based in Geneva worked throughout the Middle East as well as Afghanistan and Pakistan. In 2005, she was part of the AP team that won the Pulitzer Prize for breaking news photography for coverage of Iraq, and was awarded the Courage in Journalism Award from the International Women's Media Foundation, among many journalistic honors. In 2006-07, she studied at Harvard University under a Nieman Fellowship.

"What the world knows about Iraq, they largely know because of her pictures and the pictures by the photographers she raised and beat into shape," said AP photographer David Guttenfelder. "I know they always ask themselves, 'What would Anja do?' when they go out with their cameras. I think we all do."

"She truly believed in the need to bear witness," said Santiago Lyon, AP vice president and director of photography.

Niedringhaus captured what war meant to her subjects: an Afghan boy on a swing holding a toy submachine gun, a black-clad Iraqi giving a bottle to her baby as she waits for prisoners to be released, a U.S. Marine mourning the loss of 31 comrades.

Others showed life going on among the killing: a Canadian soldier with a sunflower stuck in his helmet, a young girl testing her artificial limbs, while her sister teasingly tries to steal her crutches, a bearded Afghan man and grinning boy listening to music on an iPod borrowed from German soldiers.

At an exhibit of her work in Berlin in 2011, Niedringhaus said: "Sometimes I feel bad because I can always leave the conflict, go back home to my family where there's no war."

Niedringhaus started her career as a freelance photographer for a local newspaper in her hometown in Hoexter, Germany, at age 16. She worked for the European Press Photo Agency before joining the AP and had published two books.

Gannon, a Canadian journalist based in Islamabad, has covered Afghanistan and Pakistan for the AP since the mid-1980s. A former Edward R. Murrow fellow at the Council on Foreign Relations in New York, she is the author of a book on the country, "I Is for Infidel: From Holy War to Holy Terror: 18 Years Inside Afghanistan." She also was awarded the Courage in Journalism Award from the International Women's Media Foundation, in 2002.

After Friday's attack, Gannon underwent surgery in Khost and was said to be stable. She was then flown to Kabul for further treatment.

Niedringhaus drew praise Friday from battlefields to the White House. She was honored at a United Nations briefing, and German Chancellor Angela Merkel's spokesman, Steffen Seibert, tweeted condolences. White House spokesman Josh Earnest said she and Gannon were in President Obama's thoughts and prayers.

In Washington, Pentagon spokesman Col. Steve Warren condemned "this senseless act of violence against these brave professionals covering this important political transition in Afghanistan."

The Committee to Protect Journalists said the loss of Niedringhaus and the wounding of Gannon "reflect the heightened dangers of reporting from Afghanistan."

"As pre-election violence mounts, Afghanistan has become a dangerous assignment on par with the height of the Iraq war or the current situation in Syria," said Bob Dietz, CPJ's Asia program coordinator.

The militants have increasingly been targeting Westerners.

Nils Horner, a 51-year-old Swedish journalist who had worked for Swedish Radio since 2001 as a foreign correspondent, was killed by a shot in the head as he was reporting on Afghanistan's election in Kabul in early March. An extremist Taliban splinter group later claimed responsibility.

On March 21, four gunmen opened fire in a crowded restaurant frequented by foreigners in the Serena Hotel in Kabul, killing nine people. Among the dead was Sardar Ahmad, a respected 40-year-old Afghan journalist with AFP. His wife and two of their children also were killed, while their nearly 2-year-old son was badly wounded.