What causes the expansion and implosion of the medium before an explosion? I often see in slo-mo explosions, whether underwater or in air, the medium expanding outward before imploding, at which point the flash of the explosion happens. I used to think this was a cool movie trope, but I see in many cases, like this and this the phenomenon is very real. Even this nuke shows a kind of implosion of the air after explosion (although I'm not sure if this is the same thing). What causes this? Is the flash of light the actual "explosion"?
 A: Background
I think it is important to recognize these movies show effects of shock waves, some of which result from blast waves.
As I showed at https://physics.stackexchange.com/a/271329/59023 and https://physics.stackexchange.com/a/242450/59023, the shock Mach number for a blast wave goes as:
$$
\begin{align}
  M\left( t \right) & = \frac{ U_{shn}\left( t \right) }{ C_{s,up} } = \frac{ 2 \ k }{ 5 \ C_{s,up} } \left( \frac{ E_{o} }{ \rho_{up} } \right)^{1/5} \ t^{-3/5} \tag{1a} \\
  & = \frac{ 2 \ k^{5/2} }{ 5 \ C_{s,up} } \sqrt{ \frac{ E_{o} }{ \rho_{up} } } \ R^{-3/2}\left( t \right) \tag{1b}
\end{align}
$$
where $t$ is time from the initial release of energy, $E_{o}$, from a point source, $\rho_{up}$ is the ambient gas mass density, $k$ is a dimensionless parameter used for scaling, $C_{s,up}$ is the upstream speed of sound (can assume this is constant, like all other upstream variables), and $R\left( t \right)$ is the radial shock position vs. time given by:
$$
R\left( t \right) = k \left( \frac{ E_{o} }{ \rho_{up} } \right)^{1/5} \ t^{2/5} \tag{2}
$$

I often see in slo-mo explosions, whether underwater or in air, the medium expanding outward before imploding...

One can see from Equations 1 and 2 that $M \propto t^{-3/5} \propto R^{-3/2}$ and will not always be greater than unity.  Meaning, there will be a point when hot gas, which acts as the shock piston in some of these cases, no longer has enough energy to maintain a shock wave.
One of the main differences between a shock produced by a blast wave and that driven by some impenetrable obstacle (in both cases, the driver is called the piston) is that the former involves a relatively narrow shocked, sheath region followed by a rarefaction region.  The blast shock thus effectively acts like a plow and piles up mass as it moves outward from the energy source leaving behind an evacuated region behind.  Thus, there is an upstream ambient pressure followed by an overpressure region (i.e., the sheath), lastly followed by the lowest pressure region of the rarefaction.
Thus, when $\left( M - 1 \right)$ becomes null and negative, the result is an implosion.

at which point the flash of the explosion happens... What causes this? Is the flash of light the actual "explosion"?

[Possible but less probable] The flash of light at the peak of the implosion may be due to something called sonoluminescence, which is not fully understood.  What we do know is that if one symmetrically compresses a gas to sufficient pressures, it can emit light.
[More probable] It may also be due to compressional heating occurring faster than it can conduct, convect, and/or radiate away resulting in a super hot gas that thermally emits electromagnetic radiation.  This is called cavitation.
Additional Details
I wrote a detailed answer that provides background on shock waves at https://physics.stackexchange.com/a/139436/59023, https://physics.stackexchange.com/a/210097/59023, https://physics.stackexchange.com/a/306184/59023, and https://physics.stackexchange.com/a/136596/59023 that you may find helpful.
References


*

*Whitham, G.B. (1999), Linear and Nonlinear Waves, New York, NY: John Wiley & Sons, Inc.; ISBN:0-471-35942-4.

