TRIPOLI, Libya — Residents here were awakened before dawn on Sunday by the sound of artillery and gunfire in the streets. When they tuned into state television broadcasts, they heard stunning news: the Libyan military had routed the rebels seeking to oust Col. Muammar el-Qaddafi. The gunfire, they were told, was in celebration.