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 $U$ be the inverse image of $Y$ in $\overline{X}$ so that we get the commutative diagram $$ \xymatrix{ X \ar[r]_j \ar[d]_f & U \ar[dl]^{f'} \ar[r]_{j'} & \overline{X} \ar[dl]^{\overline{f}} \\ Y \ar[r]_i \ar[d]_g & \overline{Y} \ar[dl]^{\overline{g}} \\ Z } $$ Then we have \begin{align*} R(g \circ f)_! & = R(\overline{g} \circ \overline{f})_* \circ (j' \circ j)_! \\ & = R\overline{g}_* \circ R\overline{f}_* \circ j'_! \circ j_! \\ & = R\overline{g}_* \circ i_! \circ Rf'_* \circ j_! \\ & = Rg_! \circ Rf_! \end{align*} The first equality is the definition of $R(g \circ f)_!$. The second equality uses the identifications $R(\overline{g} \circ \overline{f})_* = R\overline{g}_* \circ R\overline{f}_*$ and $(j' \circ j)_! = j'_! \circ j_!$ of Lemma \ref{lemma-f-shriek-composition}. The identification $i_! \circ Rf'_* \to R\overline{f}_* \circ j_!$ used in the third equality is Lemma \ref{lemma-shriek-proper-and-open}. The final fourth equality is the definition of $Rg_!$ and $Rf_!$. 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]^{f_1} \ar[r]_{j'_1} & \overline{X}_1 \ar[dl]^{\overline{f}_1} \\ Y \ar[r]_{i_1} \ar[d] & \overline{Y}_1 \ar[dl]^{\overline{g}_1} \\ Z } } \quad\text{and}\quad \vcenter{ \xymatrix{ X \ar[r]_{j_2} \ar[d] & U_2 \ar[dl]^{f_2} \ar[r]_{j'_2} & \overline{X}_2 \ar[dl]^{\overline{f}_2} \\ Y \ar[r]_{i_2} \ar[d] & \overline{Y}_2 \ar[dl]^{\overline{g}_2} \\ Z } } $$ We can first choose a compactification $i : Y \to \overline{Y}$ 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}$ 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]^{h'} \ar[ddll] \ar[rr]_{j'_1} & & \overline{X}_1 \ar[d]^h \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]^k \\ Y \ar[rr]^{i_2} \ar[d] & & \overline{Y}_2 \ar[dll] \\ Z } $$ Each of the squares $$ \xymatrix{ X \ar[r]_{j_1} \ar[d]_{\text{id}} \ar@{}[dr]|A & U_1 \ar[d]^{h'} \\ X \ar[r]^{j_2} & U_2 } \quad \xymatrix{ U_2 \ar[r]_{j_2'} \ar[d]_{f_2} \ar@{}[dr]|B & \overline{X}_2 \ar[d]^{\overline{f}_2} \\ Y \ar[r]^{i_2} & \overline{Y}_2 } \quad \xymatrix{ U_1 \ar[r]_{j_1'} \ar[d]_{f_1} \ar@{}[dr]|C & \overline{X}_1 \ar[d]^{\overline{f}_1} \\ Y \ar[r]^{i_1} & \overline{Y}_1 } \quad \xymatrix{ Y \ar[r]_{i_1} \ar[d]_{\text{id}} \ar@{}[dr]|D & \overline{Y}_1 \ar[d]^k \\ Y \ar[r]^{i_2} & \overline{Y}_2 } \quad \xymatrix{ X \ar[r]_{j_1' \circ j_1} \ar[d]_{\text{id}} \ar@{}[dr]|E & \overline{X}_1 \ar[d]^h \\ X \ar[r]^{j_2} & \overline{X}_2 } $$ gives rise to an isomorphism as follows \begin{align*} \gamma_A & : j_{2, !} \to Rh'_* \circ j_{1, !}  \\ \gamma_B & : i_{2, !} \circ Rf_{2, *} \to R\overline{f}_{2, *} \circ j'_{2, !} \\ \gamma_C & : i_{1, !} \circ Rf_{1, *} \to R\overline{f}_{1, *} \circ j'_{1, !} \\ \gamma_D & : i_{2, !} \to Rk_* \circ i_{1, !} \\ \gamma_E & : j_{2, !} \to Rh_* \circ (j'_1 \circ j_1)_! \end{align*} by applying the map from Lemma \ref{lemma-shriek-proper-and-open} (which is the same as the map in Lemma \ref{lemma-shriek-lqf-and-proper} in case the left vertical arrow is the identity). Let us write \begin{align*} F_1 & = Rf_{1, *} \circ j_{1, !} \\ F_2 & = Rf_{2, *} \circ j_{2, !} \\ G_1 & = R\overline{g}_{1, *} \circ i_{1, !} \\ G_2 & = R\overline{g}_{2, *} \circ i_{2, !} \\ C_1 & = R(\overline{g}_1 \circ \overline{f}_1)_* \circ (j'_1 \circ j_1)_! \\ C_2 & = R(\overline{g}_2 \circ \overline{f}_2)_* \circ (j'_2 \circ j_2)_! \end{align*} 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_1 \circ F_1 \to C_1$, \item $\gamma_B$ for the map $G_2 \circ F_2 \to C_2 $, \item $\gamma_A$ for the map $F_2 \to F_1$, \item $\gamma_D$ for the map $G_2 \to G_1$, and \item $\gamma_E$ for the map $C_2 \to C_1$. \end{enumerate} This implies that we have to show that the diagram $$ \xymatrix{ C_2 \ar[rr]_{\gamma_E} & & C_1 \\ G_2 \circ F_2 \ar[rr]^{\gamma_D \circ \gamma_A} \ar[u]^{\gamma_B} & & G_1 \circ F_1 \ar[u]_{\gamma_C} } $$ is commutative. We will use Lemmas \ref{lemma-shriek-proper-and-open-compose} and \ref{lemma-shriek-proper-and-open-compose-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_F \circ \gamma_A$ where $$ \xymatrix{ U_1 \ar[r]_{j'_1} \ar[d]_{h'} \ar@{}[rd]|F & \overline{X}_1 \ar[d]^h \\ U_2 \ar[r]^{j'_2} & \overline{X}_2 } $$ Thus we see that $$ \gamma_E \circ \gamma_B = \gamma_F \circ \gamma_A  \circ \gamma_B = \gamma_F \circ \gamma_B \circ \gamma_A $$ 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_C \circ \gamma_D = \gamma_F \circ \gamma_B$. Since both of these are equal to the map for the square $$ \xymatrix{ U_1 \ar[r] \ar[d] & \overline{X}_1 \ar[d] \\ Y \ar[r] & \overline{Y}_2 } $$ we conclude.