  This paper examines the mechanism of internal shocks in opaque relativistic
outflows, in particular in cosmological gamma-ray bursts. The shocks produce
neutrino emission and affect the observed photospheric radiation from the
explosion. They develop from internal compressive waves and can be of different
types depending on the composition of the outflow: (1) Shocks in "photon gas,"
with negligible plasma inertia, have a unique structure determined by the
force-free condition---zero radiation flux in the plasma rest frame. Radiation
dominance over plasma inertia suppresses formation of collisionless shocks
mediated by collective electromagnetic fields. (2) If the outflow is
sufficiently magnetized, a strong collisionless subshock develops, which is
embedded in a thicker radiation-mediated structure. (3) Waves in outflows with
a free neutron component lead to dissipation through nuclear collisions. At
large optical depths, shocks have a thickness comparable to the neutron free
path, with an embedded radiation-mediated and collisionless subshocks. The
paper also presents first-principle simulations of magnetized flows filled with
photons, demonstrating formation of shocks and their structure. Simple
estimates show that magnetized sub-photospheric shocks are efficient producers
of photons and have a great impact on the observed photospheric radiation. The
shock structure changes as the outflow expands toward its photosphere. The
dissipation is accompanied by strong $e^\pm$ pair creation, and the
$e^\pm$-dressed shock carries the photosphere with it up to two decades in
radius, emitting a strong pulse of nonthermal radiation.
