Britain's Prime Minister David Cameron is welcomed to the Presidential Palace by Afghanistan's President Ashraf Ghani on Friday in Kabul, Afghanistan. Getty Images

LONDON—British Prime Minister David Cameron flew to Afghanistan to meet with the country’s new president, Ashraf Ghani, amid an escalating military effort by the U.K. and other allies to combat the militant extremist group Islamic State in Iraq.

Mr. Cameron praised Afghanistan for the advances it had made in creating a secure and democratic country. Speaking at a joint news conference in Kabul Friday, Mr. Cameron said he and Mr. Ghani had discussed ways that the two leaders can continue to work to combat terrorism.

“The work of defeating Islamist extremist terror goes on elsewhere,” said Mr. Cameron. “And because this threatens us at home, we must continue to play our part.”

On his way to Afghanistan, Mr. Cameron visited a Royal Air Force base in Cyprus from where British Tornado jets are launching airstrikes against Islamic State militants in Iraq. He announced Friday that the U.K. is supplying two additional Tornado jets to help with the military effort.

Mr. Cameron told news media in Cyprus that he believed the strikes were making a difference but warned it would be slow going. “The ground troops are not British or American combat troops, they are Iraqi troops and it will take time,” he said. But he justified the effort by saying Islamic State, also known as ISIS or ISIL, would grow stronger if the allies weren’t engaged in airstrikes.

“ISIL is not just a threat in Iraq and Syria, it is a threat to the streets and people of Britain,” said Mr. Cameron. “That’s why we’re here, that’s why we’re doing this and I want to come and thank the people at the sharp end.”

British combat troops are due to withdraw from Afghanistan by the end of the year. Mr. Cameron Friday paid tribute to the 453 British servicemen and women who have died during the more than decade-long campaign.

“Britain has paid a heavy price for helping to bring stability to this country,” Mr. Cameron said in Kabul. “But this is where al Qaeda trained their terrorists. This is where 9/11 and countless other plots were hatched. Afghanistan free from al Qaeda is in our national interests as well as Afghanistan’s.”

Mr. Cameron is the first world leader to meet Mr. Ghani since his inauguration as president. The British Prime Minister is also due to meet Chief Executive Abdullah Abdullah while in Afghanistan.

The visit comes on the heels of the signing by the new Afghan government of a crucial security pact with the U.S. and the North Atlantic Treaty Organization, paving the way for roughly 12,000 foreign troops to remain in the country after year’s-end and ensuring that aid money keeps flowing to Kabul.

The long-delayed agreements were signed a day after Mr. Ghani took office, marking a new beginning in ties between Afghanistan and its international backers.

Combined, the security deals will allow international troops to remain in Afghanistan after December to focus on two missions: one charged with training Afghan security forces and the other to conduct counterterrorism operations. They are directly linked to the continued delivery of billions of dollars in aid that the Afghan government and its armed forces need to survive.