It may well take longer than the time you could hold your breath, and generally you start after you’ve exhaled, when you discover that no more air is available. The residual oxygen in your lungs is sufficient to sustain you as it becomes available through decompression.
Scuba diving is based on breathing compressed air. You wouldn’t be able to inhale atmospheric pressure-air at the ambient pressure below the surface. (In fact, if your chest is more than one feet deep in water, you can’t inhale atmospheric pressure-air through a tube.) One lungful of the compressed Scuba air might be equal to several lungfuls of non-compressed air. As you rise, it decompresses and expands. If you didn’t exhale, the expanding gasses would burst the lungs.
While there may not be much of lungful left at the time the situation arises, its volume increases as the pressure decreases during the ascent. Not all the oxygen we inhale is used, so there will be some oxygen there. It’s enough, I recall, to keep you supplied through the ascent.