The British embassy in Kabul confirmed Cameron's trip to Camp Bastion as Britain, which has about 7,900 soldiers serving in Afghanistan, marked its annual Armed Forces Day.
Cameron backed efforts to start peace talks with the Taliban, saying "the encouraging thing, although there is a long way to go, is that the Taliban (have) said they no longer wanted Afghanistan to pose a threat to other countries".
"There's a long way to go but alongside our security process, of a big and secure Afghan army and police force, a political process makes sense too," he said in remarks reported by the BBC.