* Clinton most senior U.S. official to visit
* Secretary of state urges creation of unified military
* Government fighters in Sirte retreat under heavy fire
* Sirte only centre of resistance after fall of Bani Walid (Edits, adds new details of battle in Sirte)
By Andrew Quinn and Tim Gaynor
TRIPOLI/SIRTE, Libya, Oct 18 (Reuters) - U.S. Secretary of State Hillary Clinton visited Libya on Tuesday to urge its disparate militias to unite around their new leaders, while loyalists of ousted Muammar Gaddafi launched a counter-attack in his hometown of Sirte.
Clinton is the most senior U.S. official to come to Tripoli since Gaddafi's 42-year rule ended in August. Her visit was marked by tight security, reflecting worries that Libya's new rulers have yet to establish full control over the country.
Speaking after meeting Libya's de facto prime minister, Mahmoud Jibril, she spoke of the need to bring the powerful and heavily-armed regional militias that emerged from the war to oust Gaddafi under central rule.
"We are encouraged by the commitment of the National Transitional Council (NTC) to take the steps necessary to bring the country together," Clinton said.
"From long experience one factor we know has to happen ... is unifying the various militias into a single military ... Getting a national army under civilian command is essential."
Though the militias express loyalty to the new government, many analysts see them as the biggest threat to Libya's unity.
The United States took part in the NATO bombing campaign that helped Libya's interim government take power, although its aircraft largely played a secondary role to those of Britain and France.
"I am proud to stand here on the soil of a free Tripoli and on behalf of the American people I congratulate Libya," Clinton said. "This is Libya's moment, this is Libya's victory, the future belongs to you."
Nearly two months since capturing Tripoli, the NTC has stamped out most pro-Gaddafi resistance but has so far failed to capture Sirte, a city on the Mediterranean coast that Gaddafi made into a showpiece for his autocratic rule.
That failure has raised questions about the NTC's ability to exert its authority over the entire country and postponed the launch of its promised democracy programme.
More on Libya
NTC celebrate capture of Bani Walid
Gaddafi determined to avoid Saddam's fate
NTC forces were a few days ago poised to declare victory in Sirte, but on Tuesday they were being forced to retreat in some places and taking intense fire from the dwindling force of Gaddafi loyalists boxed into a small area of the city.
At the eastern end of Sirte's seafront, a Reuters reporter saw the spot where, an hour earlier, mortars had landed in a cluster of NTC fighters.
Thirteen of them were killed in the incident, witnesses said. Blood from one of the victims stained the steps of a nearby house.
"A lot of martyrs have fallen," said Ahmed Al-Fitouri, of the NTC's Libya Al-Hurra brigade. "The resistance is strong."
In several places in the city, locations that a day earlier were firmly under the control of anti-Gaddafi fighters were too dangerous to access because of fire coming in from loyalist snipers.
On the edge of the "Seven hundred" district, the front line had not moved but the mood of optimism among NTC fighters had been replaced by despair at the mounting casualties.
A Reuters reporter saw two NTC fighters pull back from their firing position. They paused to warn other fighters not to advance, and as they did so, first one of the two, and then the other, were hit by rounds to the chest.
Fighters spent five minutes trying to resuscitate one of the men, then stopped when it became clear he was dead. Afterwards, they threw down sand to mop up the pool of blood from his body.
The second man, Kheir Allah Ahmed, was taken to a dressing station. He survived because the round that went through his chest missed his organs. "A sniper hit us both," he said as he underwent treatment.
Sirte is now the last major Libyan town where pro-Gaddafi forces are holding out, after the other bastion of resistance, Bani Walid, fell to the country's new rulers on Monday.
The scenes in Sirte on Tuesday were in marked contrast to events earlier this week, when Gaddafi loyalists offered little resistance as NTC forces pummelled them with tank fire and mortars.
Libya's new rulers were so confident of their imminent victory in the town that NTC chairman Mustafa Abdel Jalil visited Sirte last week and was greeted by fighters firing triumphantly into the air.
But the NTC offensive -- by mostly amateur fighters in a hotchpotch of volunteer units -- has been hampered by a lack of coordination.
Units which converged on Sirte from Benghazi in eastern Libya and Misrata to the west have lost men in "friendly fire" incidents, when they have fired at each other by mistake instead of at the Gaddafi loyalists.
Mohammed Ismail, a field commander with the anti-Gaddafi Shohada al-Thaqil brigade, said his men decided to stop bombarding loyalists with artillery and were now instead using infantry to root out snipers house by house.
"There was so much artillery firing in a small space that there was friendly fire," he said.
Another frustration for NTC leaders is that Muammar Gaddafi, wanted by the International Criminal Court on charges of ordering the killing of civilians, has not been captured. He is in hiding, possibly deep in Libya's Sahara desert.
The capture of Sirte is vital to the NTC, because it will mark the establishment of at least nominal control over all Libya's territory.
The NTC has also said the fall of Sirte will be the signal for the process to begin of creating a fully-fledged government and building democratic institutions.
That process though is fraught with risks for Libya because it will involve finding a way to divide up power between rival anti-Gaddafi groups who are impatient for a stake in the new Libya. (Additional reporting by Taha Zargoun and Rania El Gamal in Sirte, Libya, and Yasmine Saleh in Tripoli; Writing by Christian Lowe; Editing by Jon Boyle)
Our Standards: The Thomson Reuters Trust Principles.