CAIRO — Egypt’s military officers removed the country’s first democratically elected president, Mohamed Morsi, on Wednesday, suspended the Constitution and installed an interim government presided over by a senior jurist.

Tahrir Square, where tens of thousands of opponents of the government had gathered each night since Sunday to demand Mr. Morsi’s removal, erupted in fireworks and jubilation at news of the ouster. At a square near the presidential palace where Mr. Morsi’s Islamist supporters had gathered, men broke into tears and vowed to stay until he was reinstated or they were forcibly removed. “The dogs have done it and made a coup against us,” they chanted. “Dying for the sake of God is more sublime than anything,” a speaker declared.

Mr. Morsi rejected the generals’ actions as a “complete military coup.”

Military vehicles and soldiers in riot gear had surrounded the rally in the hours before the takeover, and tensions escalated through the night. Within hours, at least seven people had died and more than 300 were injured in clashes in 17 provinces between Mr. Morsi’s supporters and either civilian opponents or security forces.

By the end of the night, Mr. Morsi was in military custody and blocked from all communications, one of his advisers said, and many of his senior aides were under house arrest. Egyptian security forces had arrested at least 38 senior leaders of the Muslim Brotherhood, including Saad el-Katatni, the chief of the group’s political party, and others were being rounded up as well, security officials said. No immediate reasons were given for the detentions.