"The former president was traveling to Badakhshan province this morning and when he reached Ali Abad district, the militants targeted him with rocket propelled grenade and small arms," Hamidullah Mohtashim told journalists.
Mohtashim said that Rabbani, now a member of the country's parliament, and his convoy were unhurt while four militants were killed and 16 others were injured in the firefight between the insurgents and police and bodyguards accompanying the former president.
Taliban spokesman Zabihullah Mujahid claimed the responsibility for the attack.
He also said that Rabbani's vehicle was damaged, although the district official said Rabbani continued his journey after the shootout.
The ambush occurred prior to the country's August 20 presidential and local elections, which the Taliban has vowed to disrupt.
Rabbani is a one of the main supporters of Abdullah Abdullah, the leading rival to incumbent President Hamid Karzai in the poll.
Violence across Afghanistan is at its highest level since the Taliban were removed from power by U.S.-led forces in 2001.
Another 11 people, including women and children, were killed on Wednesday when the bus they were riding in hit a roadside bomb in southern Afghanistan.