[proofplan]
Choose a principal connection form on $P$ and pull it back along the canonical bundle map $f^*P\to P$. The curvature of this pulled-back connection is the pullback of the original curvature because exterior differentiation, wedge products, and the Lie bracket operation commute with pullback. Evaluating an invariant polynomial on curvature forms is multilinear and natural under pullback, so the Chern-Weil representative on $f^*P$ is exactly the pullback of the representative on $P$. Passing to de Rham cohomology gives the desired identity of Chern-Weil classes.
[/proofplan]
[step:Construct the pullback bundle and its pulled-back connection]
Let
\begin{align*}
f^*P:=\{(y,p)\in N\times P:f(y)=\pi(p)\}
\end{align*}
with projection
\begin{align*}
\pi_N:f^*P&\to N
\end{align*}
\begin{align*}
(y,p)&\mapsto y
\end{align*}
and canonical bundle map
\begin{align*}
\widetilde f:f^*P&\to P
\end{align*}
\begin{align*}
(y,p)&\mapsto p.
\end{align*}
The right $G$-action on $f^*P$ is
\begin{align*}
(y,p)\cdot g=(y,p\cdot g),
\end{align*}
so $\pi_N:f^*P\to N$ is a smooth principal $G$-bundle.
By hypothesis, choose a principal connection form
\begin{align*}
A\in\Omega^1(P;\mathfrak g)
\end{align*}
on $P$. Define
\begin{align*}
\widetilde A:=\widetilde f^*A\in\Omega^1(f^*P;\mathfrak g).
\end{align*}
For each fundamental vector field on $f^*P$, the map $d\widetilde f$ carries it to the corresponding fundamental vector field on $P$, and therefore $\widetilde A$ reproduces the generating [Lie algebra](/page/Lie%20Algebra) element. Also, for every $g\in G$, the identity $\widetilde f\circ R_g=R_g\circ\widetilde f$ gives
\begin{align*}
R_g^*\widetilde A
=
R_g^*\widetilde f^*A
=
\widetilde f^*R_g^*A
=
\widetilde f^*(\operatorname{Ad}_{g^{-1}}A)
=
\operatorname{Ad}_{g^{-1}}\widetilde A.
\end{align*}
Thus $\widetilde A$ is a principal connection form on $f^*P$.
[/step]
[step:Compute the curvature of the pulled-back connection]
Let
\begin{align*}
F_A:=dA+\frac{1}{2}[A\wedge A]\in\Omega^2(P;\mathfrak g)
\end{align*}
be the curvature form of $A$, and let
\begin{align*}
F_{\widetilde A}:=d\widetilde A+\frac{1}{2}[\widetilde A\wedge\widetilde A]\in\Omega^2(f^*P;\mathfrak g)
\end{align*}
be the curvature form of $\widetilde A$. Since pullback of differential forms commutes with exterior differentiation and with wedge products, and since the Lie bracket on $\mathfrak g$ is applied coefficientwise, we obtain
\begin{align*}
F_{\widetilde A}
=
d(\widetilde f^*A)+\frac{1}{2}[\widetilde f^*A\wedge\widetilde f^*A].
\end{align*}
Therefore
\begin{align*}
F_{\widetilde A}
=
\widetilde f^*(dA)+\frac{1}{2}\widetilde f^*[A\wedge A]
=
\widetilde f^*\left(dA+\frac{1}{2}[A\wedge A]\right)
=
\widetilde f^*F_A.
\end{align*}
[guided]
The point of this step is to show that the pullback connection has exactly the curvature one expects. We have defined the pulled-back connection form by
\begin{align*}
\widetilde A:=\widetilde f^*A\in\Omega^1(f^*P;\mathfrak g).
\end{align*}
Its curvature is, by definition,
\begin{align*}
F_{\widetilde A}:=d\widetilde A+\frac{1}{2}[\widetilde A\wedge\widetilde A]\in\Omega^2(f^*P;\mathfrak g).
\end{align*}
Substituting $\widetilde A=\widetilde f^*A$ gives
\begin{align*}
F_{\widetilde A}
=
d(\widetilde f^*A)+\frac{1}{2}[\widetilde f^*A\wedge\widetilde f^*A].
\end{align*}
Now we use two functorial properties of pullback forms. First, exterior differentiation is natural:
\begin{align*}
d(\widetilde f^*A)=\widetilde f^*(dA).
\end{align*}
Second, [pullback preserves wedge products](/theorems/3914). Since the bracketed wedge product $[A\wedge A]$ is obtained by wedging the form components and then applying the fixed bilinear Lie bracket $[\cdot,\cdot]:\mathfrak g\times\mathfrak g\to\mathfrak g$, the same naturality gives
\begin{align*}
[\widetilde f^*A\wedge\widetilde f^*A]=\widetilde f^*[A\wedge A].
\end{align*}
Combining these identities yields
\begin{align*}
F_{\widetilde A}
=
\widetilde f^*(dA)+\frac{1}{2}\widetilde f^*[A\wedge A]
=
\widetilde f^*\left(dA+\frac{1}{2}[A\wedge A]\right).
\end{align*}
Since
\begin{align*}
F_A:=dA+\frac{1}{2}[A\wedge A],
\end{align*}
we conclude that
\begin{align*}
F_{\widetilde A}=\widetilde f^*F_A.
\end{align*}
[/guided]
[/step]
[step:Show that evaluating the invariant polynomial commutes with pullback]
First suppose that $P_0\in I^k(G)$ is homogeneous of degree $k$, so that
\begin{align*}
P_0:\mathfrak g^k\to\mathbb R
\end{align*}
is a symmetric $\operatorname{Ad}$-invariant $k$-linear form. Its evaluation on curvature is the differential form obtained by applying $P_0$ coefficientwise to $k$ copies of the curvature form.
For tangent vectors $v_1,\dots,v_{2k}\in T_{(y,p)}(f^*P)$, multilinearity of $P_0$ and the definition of pullback give
\begin{align*}
P_0(F_{\widetilde A})(v_1,\dots,v_{2k})
=
P_0(\widetilde f^*F_A)(v_1,\dots,v_{2k})
=
(\widetilde f^*P_0(F_A))(v_1,\dots,v_{2k}).
\end{align*}
Hence
\begin{align*}
P_0(F_{\widetilde A})=\widetilde f^*P_0(F_A)
\end{align*}
as forms on $f^*P$. If $P_0\in I(G)$ is not homogeneous, decompose it into its homogeneous components and apply the same argument componentwise.
[/step]
[step:Descend the equality to the base manifolds and pass to cohomology]
Because $P_0$ is $\operatorname{Ad}$-invariant, the Chern-Weil form $P_0(F_A)$ is basic on $P$, and the Chern-Weil form $P_0(F_{\widetilde A})$ is basic on $f^*P$. Thus there are unique forms on the bases, denoted
\begin{align*}
P_0(F_A)_M\in\Omega^{\mathrm{even}}(M)
\end{align*}
and
\begin{align*}
P_0(F_{\widetilde A})_N\in\Omega^{\mathrm{even}}(N),
\end{align*}
whose pullbacks to the total spaces are $P_0(F_A)$ and $P_0(F_{\widetilde A})$, respectively. The equality
\begin{align*}
P_0(F_{\widetilde A})=\widetilde f^*P_0(F_A)
\end{align*}
is precisely the pullback to $f^*P$ of
\begin{align*}
P_0(F_{\widetilde A})_N=f^*\bigl(P_0(F_A)_M\bigr).
\end{align*}
By the definition of the Chern-Weil homomorphism in [citetheorem:9765],
\begin{align*}
\operatorname{cw}_{P}(P_0)=[P_0(F_A)_M]\in H^{\mathrm{even}}_{\mathrm{dR}}(M)
\end{align*}
and
\begin{align*}
\operatorname{cw}_{f^*P}(P_0)=[P_0(F_{\widetilde A})_N]\in H^{\mathrm{even}}_{\mathrm{dR}}(N).
\end{align*}
Since pullback of differential forms sends closed forms to closed forms and exact forms to exact forms, it induces the usual pullback map on de Rham cohomology. Therefore
\begin{align*}
\operatorname{cw}_{f^*P}(P_0)
=
[P_0(F_{\widetilde A})_N]
=
[f^*(P_0(F_A)_M)]
=
f^*[P_0(F_A)_M]
=
f^*\operatorname{cw}_{P}(P_0).
\end{align*}
This proves the asserted naturality identity.
[/step]