The answer by J_Lopez8 gives a motivation (and a proof) for the "mysterious" formula. But knowing the formula, you can prove it using residue theory.
As $f'\ne 0$ in a nhood of $P$, for $r$ small enough the integrand has at most a simple pole in $f^{−1}(w)$ with residue
$$
{\rm Res}_{s=f^{-1}(w)}{\frac{sf'(s)}{f(s) - w}} = \lim_{s\to f^{-1}(w)}(s - f^{-1}(w))\frac{sf'(s)}{f(s) - w}
= \lim_{s\to f^{-1}(w)}\frac{{sf'(s)}}{\frac{f(s) - f(f^{-1}(w))}{s - f^{-1}(w)}}
= \frac{f^{-1}(w)f'(f^{-1}(w))}{f'(f^{-1}(w))} = f^{-1}(w).
$$
For an alternative proof (Complex Functions by Goodstein, 8.2.8), use the following theorem:
Let be $f:D\longrightarrow{\Bbb C}$ holomorphic in the simply connected open set $D$, $\gamma\subset D$ a simple closed curve, $f'(z)\ne 0$ for all $z\in\gamma$. If $f$ has a only simple pole (has only a pole and is simple) $z_0$ in the interior of $\gamma$, then
$$z_0 = \frac1{2\pi i}\int_\gamma\frac{z f'(z)}{f(z)}\,dz.$$