CAIRO — Egypt’s new government lost control of a major city, Port Said, on Saturday as rampaging soccer fans attacked the main jail, drove police officers from the streets and cut off all access to the city.

Set off by the sentencing of 21 Port Said soccer fans to death, the rioting was the sharpest challenge yet to the efforts of Egypt’s new Islamist rulers to re-establish order after the two years of turmoil that have followed the overthrow of Hosni Mubarak, Egypt’s autocratic president.

By evening, fighting in the streets had left at least 30 people dead, mostly from gunfire, and injured more than 300. Residents said they were afraid to leave their homes. Doctors said the local hospital was overloaded with casualties and pleaded for help. Rioters sacked and burned a police barracks; attacked police stations, the Port Said power plant and the jail, where the convicted men were being held; and closed off all roads to the city as well as the railroad station.

President Mohamed Morsi canceled a foreign trip to deal with the crisis at home and instead met with the National Defense Council, which includes the nation’s top military leaders. A spokesman for the Interior Ministry acknowledged that its security forces were unable to control the violence and urged that political leaders to try to calm the rioters.