The "magic diagram" is cartesian I am trying to solve an exercise from Vakil's lecture notes on algebraic geometry, namely, I want to show that
$\require{AMScd}$
\begin{CD}
    X_1\times_Y X_2 @>>> X_1\times_Z X_2\\
    @V V V @VV V\\
    Y @>>> Y \times_Z Y
\end{CD}
is a cartesian diagram. I think I've figured out where the maps come from: The lower map is the diagonal induced from the universal property of $Y \times_Z Y$, the upper map is coming from the universal property of $X_1\times_Z X_2$ (this is one of the previous exercises in Vakil's notes), the map on the right also comes from the universal property of $Y \times_Z Y$ since we have two maps from $X_1\times_Z X_2$ to $Y$ whose composition with $Y\rightarrow Z$ coincides, and finally the map on the left side is either composition $X_1\times_Y X_2 \rightarrow X_i \rightarrow Y$. At this point, I don't even see why this diagram should be commutative.
But even discarding the issue of commutativity, given some $T$ instead of $X_1\times_Y X_2$ making the above diagram commute, I have no clue how to obtain a canonical map $T \rightarrow X_1\times_Y X_2$.
I could solve all the other problems in this section of his notes, but this one eludes me. Any help or solution (preferably just using the universal properties that we're given) would be greatly appreciated.
 A: Here are some hints. First of all, when things seem really unclear, it helps to consider the category of sets. In this case, the set $X_1 \times_Y X_2$ consists of pairs $(x_1, x_2)$ which have the same image in $Y,$ and the composite $X_1 \times_Y X_2 \to Y \to Y \times_Z Y$ sends $(x_1, x_2) \mapsto (y, y),$ where $y$ is the common image of $x_1$ and $x_2.$ On the other hand, $X_1 \times_Z X_2$ consists of pairs $(x_1, x_2)$ where the $x_i$ have the same image in $Z$. In particular, this set naturally contains $X_1 \times_Y X_2,$ and the composite $X_1 \times_Y X_2 \to X_1 \times_Z X_2 \to Y \times_Z Y$ is $(x_1, x_2) \mapsto (y, y)$ - the same as before! So we have commutativity on the level of sets, and the same thing "must work" on the level of schemes as well. Just rephrase using the functor of points or universal properties (this is admittedly a bit cumbersome).
But you want to show that the square is cartesian. Well again, I'm sure this can be done using universal properties arguments (or functor of points, probably more easily), but sometimes it can be easier to just get down to the dirty, so to speak. By recalling the definition of the fibre product, it is more or less obvious that you can reduce to the affine case. So let $A, B, C_1, C_2$ be rings. You want to show that $B \otimes_{B \otimes_A B} (C_1 \otimes_A C_2) \approx C_1 \otimes_B C_2.$ Again, there is the elegant way and the brute force way. For the latter, you note that the left side is generated by tensors of the form $b \otimes (c_1 \otimes c_2).$ But you can use the $B \otimes_A B$-algebra structure as follows: $b \otimes (c_1 \otimes c_2) = (b \otimes 1) [1 \otimes (c_1 \otimes c_2)] = 1 \otimes (b \otimes 1)(c_1 \otimes c_2) = 1 \otimes (bc_1 \otimes c_2).$ By using $1 \otimes b$ instead you arrive at $c_1 \otimes bc_2.$ In any case, you now have an obvious map on generators, and just have to show that the relations correspond.
This is the nitty gritty of it. As I have said several times, the argument can be cut short at many points by replacing an explicit check using the construction by a universal properties argument, and this is probably the way to go. The above explicit work may help you to find the elegant proof.
A: First, why is the diagram commutative: you've got the following commutative diagram:

It is commutative precisely because this is how we defined the map $X_1 \times_Y X_2 \to X_1 \times_Z X_2$. The bottom right square is used to define $Y \to Y \times_Z Y$.
Now, you diagram is commutative iff the two maps $X_1 \times_Y X_2 \to Y \times_Z Y$ are equal, iff each component maps are equal.


*

*The red path is used to define the (first component of the) map that factors through $X_1 \times_Y X_2 \to X_1 \times_Z X_2 \to Y \times_Z Y$

*The blue path is used to define the (first component of the) map that factors through $X_1 \times_Y X_2 \to Y \to Y \times_Z Y$.


As you can see, they are equal. Therefore the magic diagram commutes.

Now, the universal property. Suppose you're given $T \to X_1 \times_Z X_2$ and $T \to Y$  such that the two maps $T \to Y \times_Z Y$ are equal. In other words, you're given maps $T \to X_1$, $T \to X_2$ and $T \to Y$, such that the two maps $T \to X_i \to Z$ are equal, and the maps the blue path and the red path are equal (where $T$ is in the position of $X_1 \times_Y X_2$). As you can see, this is precisely the same thing as giving two maps $T \to X_i$ such that $T \to X_i \to Y$ are equal, because then the fact that the maps into $Z$ are equal is a consequence of the fact that the maps into $Y$ are equal. So there's a unique map $T \to X_1 \times_Y X_2$ making everything commute.

Considering that Vakil introduces the Yoneda lemma after this exercise, I didn't dare give the proof using it. But by the Yoneda lemma, you can just consider the case of sets; in the equations that you'll write, you'll arrive at the same conclusion -- that giving a maps that make everything commute will give you a redundant condition that the images in $Z$ are the same, and that the fibered product of the whole thing is the fibered product over $Y$.
A: I want to add a very fast proof to the already existing answers. Let me assume that the category $\mathcal C$ has finite limits. We like to show that each magic-square is a pullback in $\mathcal C$.
The Yoenda-embedding is fully faithful and thus reflects limits. Hence if we can show each magic-square is a pullback in $Pr(\mathcal C)$ after we apply the Yoneda-embedding $y:\mathcal C\to Pr(\mathcal C)$ to it, then we are done. Since the Yoneda-embedding also preserves all limits, we see that if we can show the magic-square theorem for the category $Pr(\mathcal C)$, then it applies in particular to those which we obtained from $\mathcal C$ via the Yoneda-embedding. Hence, we have shown that it is enough to prove the magic-square theorem in the category $Pr(\mathcal C)$.
Next, consider the functor $U:Pr(\mathcal C)\to \prod_{Ob(\mathcal C)} Set$ which takes a presheaf $X$ to the family $(X(c))_{c\in Ob(\mathcal C)}$. It is well-known that the functor $U$ preserves and reflects limits. In category theory text books this result is usually formulated as follows: limits of presheaves are constructed pointwise. Now by the same argument as before, we see that it is enough to prove the magic-square theorem in $\prod_{c\in Ob(\mathcal C)}Set$. But now we can work in each of the components separately, since limits in a product of categories are constructed and can be checked entry-wise. Hence, it is enough to prove the magic square theorem in $Set$, where we can use elements to make the argument.
