"This accord opens the door to the future cooperation and partnership between Italy and Libya," Libyan leader Muammar Gaddafi said at the signing ceremony at a palace which was once the headquarters of the Rome government's senior official during the 1911-1943 colonial rule.
Italy has had difficult relations with Gaddafi since he took power in 1969 but has backed Tripoli's recent drive to mend fences with the West. The "friendship pact" removes a major hurdle to an improvement in ties.
Italian Prime Minister Silvio Berlusconi said the accord ends "40 years of misunderstanding", adding that "it is a complete and moral acknowledgement of the damage inflicted on Libya by Italy during the colonial era".
"In the name of the Italian people ... I feel the duty to apologize and show our pain for what happened many years ago and which affected many of your families," Berlusconi said, according to a text on the government's website.
Libya says Italian troops killed thousands of Libyans and drove thousands more from their villages and cities during the colonial era.
"In this historic document, Italy apologizes for its killing, destruction and repression against Libyans during the colonial rule," Gaddafi said.
Present day Italy is a friendly country, added Gaddafi, who expelled Italian residents and confiscated their property in 1970.
Gaddafi gave no details of the amount of money involved in the deal but Berlusconi said on arrival that $200 million per year will be invested by Italy in Libya over 25 years.
"Italian companies will set up more business in Libya," Berlusconi said, without giving details.
Italian officials said the amount of compensation would total $5 billion in investments, including the construction of a highway across Libya from the Tunisian border to Egypt.
It also involves a project to clear mines dating back to the colonial era.
Italy expects in return to win energy contracts and for the Tripoli government to toughen security measures, including joint maritime patrols, to stem the flow of illegal migrants.
In a goodwill gesture on Saturday, Italy returned an ancient statue of Venus taken to Rome during colonial rule, Libyan state media reported.
The headless "Venus of Cyrene" was carried away from the town of Cyrene, an ancient Greek colony, by Italian troops and put on display in Rome.
Tripoli's relations with the West have improved dramatically since 2003 when Libya accepted responsibility for the 1988 bombing of Pan Am Flight 103 over Lockerbie, Scotland.
Libya has also said it would stop pursuing nuclear, chemical and biological weapons.
On August 14 Libya signed a deal with the United States to settle both countries' claims for compensation for bombings.