When p and n junction is joined end to end , there is diffusion of electrons to the p region and holes to the n region. This creates immobile ions and internal electric field arise due to the immobile ions. The electric field prevents further diffussion of charge carriers and this region void of charge carriers is known as the depletion region.
When the positive terminal of battery is connected to the p type end and negative to the n type end of the pn junction a external electric field greater than the internal electric field enables a flow of charge carrier narrowing the depletion region.
When the battery polarity is reversed the depletion region widens hence no flow of charge carriers.