BP’s effort to contain the oil spill in the Gulf of Mexico suffered another setback on Wednesday when a discharge of liquid and gases forced the company to remove the containment cap that for three weeks had been able to capture much of the oil gushing from its damaged well.

Adm. Thad W. Allen of the Coast Guard, at a briefing in Washington, said a remote-controlled submersible operating a mile beneath the surface had most likely bumped a vent and compromised the system. Live video from the sea floor showed oil and gas storming out of the well unrestricted.

By evening, the cap was back on, nestled in place on the eighth try after about 90 minutes of effort. Live video showed remote-controlled submersibles frequently moving hoses out of the way so that the cap could be lowered over the spewing oil.

The company said the funneling of oil and gas through a pipe to the drill ship Discoverer Enterprise began shortly after the cap was properly positioned. John Curry, a BP spokesman, said collection would return to full capacity “as conditions permit.”