Choose a compactification $i : Y \to \overline{Y}$ of $Y$ over $Z$. Choose a compactification $X \to \overline{X}$ of $X$ over $\overline{Y}$. This uses More on Flatness, Theorem \ref{flat-theorem-nagata} and Lemma \ref{flat-lemma-compactifyable} twice. Let $\overline{a}$ be the right adjoint of Lemma \ref{lemma-twisted-inverse-image} for $\overline{X} \to \overline{Y}$ and let $\overline{b}$ be the right adjoint of Lemma \ref{lemma-twisted-inverse-image} for $\overline{Y} \to Z$. Then $\overline{a} \circ \overline{b}$ is the right adjoint of Lemma \ref{lemma-twisted-inverse-image} for the composition $\overline{X} \to Z$. Hence $g^! = i^* \circ \overline{b}$ and $(g \circ f)^! = (X \to \overline{X})^* \circ \overline{a} \circ \overline{b}$. Let $U$ be the inverse image of $Y$ in $\overline{X}$ so that we get the commutative diagram $$ \xymatrix{ X \ar[r]_j \ar[d] & U \ar[dl] \ar[r]_{j'} & \overline{X} \ar[dl] \\ Y \ar[r]_i \ar[d] & \overline{Y} \ar[dl] \\ Z } $$ Let $\overline{a}'$ be the right adjoint of Lemma \ref{lemma-twisted-inverse-image} for $U \to Y$. Then $f^! = j^* \circ \overline{a}'$. We obtain $$ \gamma : (j')^* \circ \overline{a} \to \overline{a}' \circ i^* $$ by (\ref{equation-sheafy}) and we can use it to define $$ (g \circ f)^! = (j' \circ j)^* \circ \overline{a} \circ \overline{b} = j^* \circ (j')^* \circ \overline{a} \circ \overline{b} \to j^* \circ \overline{a}' \circ i^* \circ \overline{b} = f^! \circ g^! $$ which is an isomorphism on objects of $D_\QCoh^+(\mathcal{O}_Z)$ by Lemma \ref{lemma-proper-noetherian}. To finish the proof we show that this isomorphism is independent of choices made. \medskip\noindent Suppose we have two diagrams $$ \vcenter{ \xymatrix{ X \ar[r]_{j_1} \ar[d] & U_1 \ar[dl] \ar[r]_{j'_1} & \overline{X}_1 \ar[dl] \\ Y \ar[r]_{i_1} \ar[d] & \overline{Y}_1 \ar[dl] \\ Z } } \quad\text{and}\quad \vcenter{ \xymatrix{ X \ar[r]_{j_2} \ar[d] & U_2 \ar[dl] \ar[r]_{j'_2} & \overline{X}_2 \ar[dl] \\ Y \ar[r]_{i_2} \ar[d] & \overline{Y}_2 \ar[dl] \\ Z } } $$ We can first choose a compactification $i : Y \to \overline{Y}$ with dense image of $Y$ over $Z$ which dominates both $\overline{Y}_1$ and $\overline{Y}_2$, see More on Flatness, Lemma \ref{flat-lemma-compactifications-cofiltered}. By More on Flatness, Lemma \ref{flat-lemma-right-multiplicative-system} and Categories, Lemmas \ref{categories-lemma-morphisms-right-localization} and \ref{categories-lemma-equality-morphisms-right-localization} we can choose a compactification $X \to \overline{X}$ with dense image of $X$ over $\overline{Y}$ with morphisms $\overline{X} \to \overline{X}_1$ and $\overline{X} \to \overline{X}_2$ and such that the composition $\overline{X} \to \overline{Y} \to \overline{Y}_1$ is equal to the composition $\overline{X} \to \overline{X}_1 \to \overline{Y}_1$ and such that the composition $\overline{X} \to \overline{Y} \to \overline{Y}_2$ is equal to the composition $\overline{X} \to \overline{X}_2 \to \overline{Y}_2$. Thus we see that it suffices to compare the maps determined by our diagrams when we have a commutative diagram as follows $$ \xymatrix{ X \ar[rr]_{j_1} \ar@{=}[d] & & U_1 \ar[d] \ar[ddll] \ar[rr]_{j'_1} & & \overline{X}_1 \ar[d] \ar[ddll] \\ X \ar'[r][rr]^-{j_2} \ar[d] & & U_2 \ar'[dl][ddll] \ar'[r][rr]^-{j'_2} & & \overline{X}_2 \ar[ddll] \\ Y \ar[rr]^{i_1} \ar@{=}[d] & & \overline{Y}_1 \ar[d] \\ Y \ar[rr]^{i_2} \ar[d] & & \overline{Y}_2 \ar[dll] \\ Z } $$ and moreover the compactifications $X \to \overline{X}_1$ and $Y \to \overline{Y}_2$ have dense image. We use $\overline{a}_i$, $\overline{a}'_i$, $\overline{c}$, and $\overline{c}'$ for the right adjoint of Lemma \ref{lemma-twisted-inverse-image} for $\overline{X}_i \to \overline{Y}_i$, $U_i \to Y$, $\overline{X}_1 \to \overline{X}_2$, and $U_1 \to U_2$. Each of the squares $$ \xymatrix{ X \ar[r] \ar[d] \ar@{}[dr]|A & U_1 \ar[d] \\ X \ar[r] & U_2 } \quad \xymatrix{ U_2 \ar[r] \ar[d] \ar@{}[dr]|B & \overline{X}_2 \ar[d] \\ Y \ar[r] & \overline{Y}_2 } \quad \xymatrix{ U_1 \ar[r] \ar[d] \ar@{}[dr]|C & \overline{X}_1 \ar[d] \\ Y \ar[r] & \overline{Y}_1 } \quad \xymatrix{ Y \ar[r] \ar[d] \ar@{}[dr]|D & \overline{Y}_1 \ar[d] \\ Y \ar[r] & \overline{Y}_2 } \quad \xymatrix{ X \ar[r] \ar[d] \ar@{}[dr]|E & \overline{X}_1 \ar[d] \\ X \ar[r] & \overline{X}_2 } $$ is cartesian (see More on Flatness, Lemma \ref{flat-lemma-compactifications-cofiltered} part (c) for A, D, E and recall that $U_i$ is the inverse image of $Y$ by $\overline{X}_i \to \overline{Y}_i$ for B, C) and hence gives rise to a base change map (\ref{equation-sheafy}) as follows $$ \begin{matrix} \gamma_A : j_1^* \circ \overline{c}' \to j_2^* & \gamma_B : (j_2')^* \circ \overline{a}_2 \to \overline{a}'_2 \circ i_2^* & \gamma_C : (j_1')^* \circ \overline{a}_1 \to \overline{a}'_1 \circ i_1^* \\ \gamma_D : i_1^* \circ \overline{d} \to i_2^* & \gamma_E : (j'_1 \circ j_1)^* \circ \overline{c} \to (j'_2 \circ j_2)^* \end{matrix} $$ Denote $f_1^! = j_1^* \circ \overline{a}'_1$, $f_2^! = j_2^* \circ \overline{a}'_2$, $g_1^! = i_1^* \circ \overline{b}_1$, $g_2^! = i_2^* \circ \overline{b}_2$, $(g \circ f)_1^! = (j_1' \circ j_1)^* \circ \overline{a}_1 \circ \overline{b}_1$, and $(g \circ f)^!_2 = (j_2' \circ j_2)^* \circ \overline{a}_2 \circ \overline{b}_2$. The construction given in the first paragraph of the proof and in Lemma \ref{lemma-shriek-well-defined} uses \begin{enumerate} \item $\gamma_C$ for the map $(g \circ f)^!_1 \to f_1^! \circ g_1^!$, \item $\gamma_B$ for the map $(g \circ f)^!_2 \to f_2^! \circ g_2^!$, \item $\gamma_A$ for the map $f_1^! \to f_2^!$, \item $\gamma_D$ for the map $g_1^! \to g_2^!$, and \item $\gamma_E$ for the map $(g \circ f)^!_1 \to (g \circ f)^!_2$. \end{enumerate} We have to show that the diagram $$ \xymatrix{ (g \circ f)^!_1 \ar[r]_{\gamma_E} \ar[d]_{\gamma_C} & (g \circ f)^!_2 \ar[d]_{\gamma_B} \\ f_1^! \circ g_1^! \ar[r]^{\gamma_A \circ \gamma_D} & f_2^! \circ g_2^! } $$ is commutative. We will use Lemmas \ref{lemma-compose-base-change-maps} and \ref{lemma-compose-base-change-maps-horizontal} and with (abuse of) notation as in Remark \ref{remark-going-around} (in particular dropping $\star$ products with identity transformations from the notation). We can write $\gamma_E = \gamma_A \circ \gamma_F$ where $$ \xymatrix{ U_1 \ar[r] \ar[d] \ar@{}[rd]|F & \overline{X}_1 \ar[d] \\ U_2 \ar[r] & \overline{X}_2 } $$ Thus we see that $$ \gamma_B \circ \gamma_E = \gamma_B \circ \gamma_A  \circ \gamma_F = \gamma_A \circ \gamma_B \circ \gamma_F $$ the last equality because the two squares $A$ and $B$ only intersect in one point (similar to the last argument in Remark \ref{remark-going-around}). Thus it suffices to prove that $\gamma_D \circ \gamma_C = \gamma_B \circ \gamma_F$. Since both of these are equal to the map (\ref{equation-sheafy}) for the square $$ \xymatrix{ U_1 \ar[r] \ar[d] & \overline{X}_1 \ar[d] \\ Y \ar[r] & \overline{Y}_2 } $$ we conclude.