David Cameron has told Libyans during a surprise visit on Thursday that "the British people want to stand with you".
On the second stop of a trip to Africa, the prime minister was greeted in Martyrs' Square in Tripoli, after speaking to recruits at a police training college.
He also met Prime Minister Ali Zidan and President Mohamed Magarief.
Downing Street had requested a news blackout ahead of the prime minister's arrival from Algeria for security reasons.
Mr Cameron told police recruits at the training centre, which is receiving support from the British government, that it was "very good to be back".
In September 2011, Mr Cameron travelled to Libya with President Nicolas Sarkozy to celebrate the liberation of this country from Colonel Gaddafi.
"I will never forget the scenes I saw in Tripoli and Benghazi," he said.
"The British people want to stand with you and help you deliver the greater security that Libya needs.
Mr Cameron announced on Thursday that police investigating the 1988 Lockerbie bombing are to visit Libya.
Officers from the Dumfries and Galloway force had been granted permission to visit the country, he said.
Visiting Algeria on Wednesday in the first leg of his trip to Africa, the prime minister said the international community should use "everything at its disposal" to fight terrorism.
The recent hostage crisis at the In Amenas gas plant, in which some 37 foreigners died, was "a reminder that what happens in other countries affects us at home", he said.
Mr Cameron also defended Western intervention in the conflict in Mali.
He was the first British prime minister to visit Algeria since it became independent in 1962.