Context. The focal-plane contrast of exoplanet imagers is affected by non-common path aberrations (NCPAs) that the adaptive optics system cannot correct for because they occur after the wavefront has been measured. NCPA estimation is commonly based on the long-exposure science image. Phase retrieval algorithms are often used, and they mostly assume that the residual phase error right after the adaptive optics system and averaged over the integration time is zero. This assumption is not always correct, for instance when controlling the adaptive optics to maximize the focal-plane contrast at the location of an exoplanet, that is to say in an adaptive coronagraph. For such cases, we present a method to calculate the NCPA using the phase information derived from the wavefront sensor (WFS) data and the science focal-plane image. Aims: We aim to accurately estimate the NCPA phase in the presence of (residual) atmospheric turbulence with a nonzero average wavefront. We then aim to take the NCPA into account in the adaptive coronagraph controller and achieve a higher contrast. Methods: The WFS measures the wavefront throughout the integration time of the science image. We combine information from the recorded WFS phases to remove the effects of the nonzero average phase from the Point Spread Function (PSF) and to remove the effects of the residual turbulence averaging over time. Then we estimate the NCPA by applying a phase-diversity- based algorithm to the resulting images. Our method is currently limited to imagers with pupil-plane coronagraphs. Results: We are able to recover the NCPA in an adaptive coronagraph setting with 0.01 radian RMS residuals and with a residual turbulence phase error of approximately 0.4 radian RMS. When accounted for in a contrast-control scheme, the NCPA correction leads to an order of magnitude improvement of contrast and a 50% increase in Strehl ratio, in numerical simulations.