* Anti-aircraft gunfire and explosions heard near Benghazi
* Government set deadline for rebels to end oil blockade
* Nascent army takes on experienced militia (Adds details on fighting)
By Ayman al-Warfalli
BENGHAZI, Libya, March 22 (Reuters) - Libyan rebels occupying oil ports clashed with troops on Saturday after attacking an army base where reinforcements were preparing for an offensive to break the blockade, local residents said.
Anti-aircraft gunfire and explosions were heard late at night and again after dawn on Saturday in Ajdabiya, the hometown of rebel leader Ibrahim Jathran, whose fighters seized three ports in summer to demand a greater share in Libya's oil wealth.
Fighting broke out just hours before the return to Libya of an oil tanker seized on Sunday by U.S. commandos in the Mediterranean after it had loaded crude at one of the ports Jathran's men have occupied.
The Morning Glory, once a North Korean-flagged vessel, was expected to arrive on Saturday in Libya under U.S. Navy escort and dock at Zawiya, one of the country's key ports under government control.
There was no immediate confirmation from hospitals of any casualties after rebels opened fire with anti-aircraft canons and rocket-propelled grenades on the army base. Residents said nearby homes were hit by several shells in the attack.
"You can hear heavy shooting at several places in Ajdabiya," said one resident.
The struggle for control of Libya's vital petroleum resources is one of the challenges facing the weak central government, which has been unable to secure the North African country three years after the fall of Muammar Gaddafi.
Brigades of former anti-Gaddafi rebels and militias refuse to disarm and often use armed force or control over oil facilities to make demands on a state whose national army is still in training.
Tripoli's central government gave Jathran a two-week deadline on March 12 to end his port blockade or face a military assault, though analysts say Libya's nascent armed forces may struggle to carry out that threat.
Jathran's self-declared Cyrenaica government, which is demanding more autonomy for his eastern region and a share of oil revenues, could not immediately be reached.
Government officials also did not return calls seeking comment on the fighting. State news agency LANA only ran a brief story late on Friday, with the army's chief of staff warning citizens not to approach any military installation.
After months of threats, Jathran's federalist gunmen managed to load crude onto the Morning Glory tanker. The ship left port and escaped Libya's navy, embarrassing Tripoli's government and prompting parliament to sack Prime Minister Ali Zeidan.
The seizure of the Morning Glory tanker in international waters was a rare boost for Tripoli's government, which has struggled to end the standoff that has cost the state more than $7 billion in lost oil revenue.
The three ports account for around 700,000 barrels per day of Libya's oil export capacity, or around half of its total petroleum shipments.
The town where Saturday's battle broke out, Ajdabiya, is divided between Jathran supporters and those who fear his oil blockade will lead to the collapse of the state.
But any major assault on the three ports may bolster support for Jathran's cause for a federalist state, allowing his eastern region more say in controlling its own affairs and spending local oil revenues.
Tripoli's government is also paralysed by infighting among Islamists, secular parties and tribes that has delayed the country's transition to democracy since the fall of Gaddafi whose one-man rule left few state institutions.
Western governments, which supported NATO's air strikes to help the 2011 anti-Gaddafi rebellion, are training the country's armed forces and pressing the factions to reach a political settlement to assure more stability. (Reporting by Ayman al-Warfalli; Writing by Ulf Laessing and Patrick Markey; Editing by Tom Heneghan)