The BBC's Barbara Plett in Islamabad says militants based in Pakistan's border regions have threatened to kill various government ministers, and have carried out deadly suicide bombings against army and government targets.
The incident represents a major lapse in security. In December former Prime Minister Benazir Bhutto was killed at an election rally in Rawalpindi.
Two bullets hit the window of the armoured vehicle on the driver's side but did not penetrate the glass and there are no reports of injuries.
"I spoke to the prime minister, and by the grace of God, he is safe and sound," Pakistan's Interior Ministry chief Rehman Malik said.
"This is a cowardly act... We have been aware of the threats against his person and our security is always tight," he said, but gave no details of whether Mr Gilani was in the car at the time of the attack.
Earlier the PM's press secretary, Zahid Bashir, told the BBC: "The prime minister was coming back from Lahore. The firing took place on the Islamabad highway. At this point, we believe the firing was from a small hill on the roadside."
Officials say another car in the convoy was also hit by several bullets. There were no reports of injuries.
An apparent assassination attempt so close to the capital has alarmed the government, which has launched a high-level inquiry, our correspondent says.
She says suspicion could fall on the Pakistani Taleban, who may have tried to take revenge for a recent army offensive that killed hundreds of militants.
Another likely suspect is al-Qaeda, the group blamed for assassinating Ms Bhutto, who led the Pakistan People's Party (PPP) to which Mr Gilani belongs, our correspondent says.
Mr Gilani became prime minister after February's elections
Mr Gilani had been in Lahore to canvass support for Asif Zardari, Ms Bhutto's widower, ahead of presidential elections on Saturday.
Ms Bhutto had been favourite to win Pakistan's general elections and become prime minister for a third time before she was killed on 27 December. The elections were subsequently postponed until February.
Her PPP emerged as the winners and formed a coalition with the PML-N party of another former Prime Minister, Nawaz Sharif.
The coalition broke up amid political acrimony late last month.
The PPP and the PML-N spent much of their time since February arguing over issues such as the power of the presidency and the reinstatement of judges sacked by former President Pervez Musharraf.
During that time the economy has taken a further battering, with the Pakistani rupee falling to an all-time low, while food and fuel prices have risen sharply.
The recent army operation against Islamist militants has been in the district of Bajaur and is estimated to have displaced up to 300,000 local people.
This week the government said the operation would be suspended during the holy month of Ramadan.
Last year militants grew increasingly confident and carried out a series of attacks in Islamabad and Rawalpindi, the country's main garrison town.
And last month a double suicide attack at a munitions factory in the town of Wah in Punjab province left nearly 70 people dead.
The loose alliance of militants that calls itself the Pakistan Taleban claimed responsibility for the Wah incident, the heaviest attack on a military installation by a militant group in the country's history.
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.