The formula can be derived from two geometric results/theorems:
Parallelogram law
In a parallelogram $ABCD$ with $AB = CD = a, BC = DA = b$, one has $$AC^2 + BD^2 = 2(AB^2 + BC^2) = 2(a^2+b^2)$$
Since in a parallelogram, $\angle A = \angle C$ and $\angle B = \angle D = \pi - \angle A$, one can easily derive this result from law of cosines.
The second result concerns parallel lines in space.
Two lines in space are parallel if either they are the same line or they lie in a common plane and didn't intersect. The results we need is "parallel-ness" among lines is transitive:
Given any three lines $a, b, c$; if $a$ is parallel to $b$ and $b$ is parallel to $c$, then $a$ is parallel to $c$.
$$a \parallel b\quad\text{ and }\quad b \parallel c\quad\implies\quad a \parallel c
$$
This can be proved from first principle using Hilbert's axioms.
For a proof, see this answer.
Back to the original problem.
Let $\mathcal{P}$ be a parallelepiped with sides $a,b,c$. Let $O$ be a vertex of $\mathcal{P}$. Let $A,B,C$ be the three vertices adjacent to $O$ such that
$$|OA| = a, |OB| = b, |OC| = c, \angle BOC = \alpha, \angle COA = \beta, \angle AOB = \gamma$$
Let $A_1,B_1,C_1,D$ be the remaining 4 vertices of $\mathcal{P}$ oppositie to $A, B, C$ and $O$ respectively.
Being a parallelepiped, it faces are parallelograms. In particular,
$OAB_1C$ and $AC_1DC$ are parallelograms. This implies
$$OC \parallel AB_1, |OC| = |AB_1| \quad\text{ and }\quad AB_1 \parallel CD_1, |AB_1| = |C_1D|$$
By second result, $OC \parallel CD_1, |OC| = |CD_1|$ and $OC_1DC$ is a parallelogram. By a similiar arguments, $OA_1DA$ and $AC_1A_1C$ are parallelograms too.
Apply parallelogram law to parallelograms $OC_1DC, OA_1DA, AC_1A_1C, OAB_1C$, we obtain
$$\begin{align}
OD^2 + CC_1^2 &= 2(OC^2 + OC_1^2)\\
OD^2 + AA_1^2 &= 2(OA^2 + OA_1^2)\\
AA_1^2 + CC_1^2 &= 2(AC^2 + AC_1^2) = 2(AC^2 + OB^2)\\
AC^2 + OB_1^2 &= 2(OA^2 + OC^2)\\
\end{align}$$
Sum the $1^{st}$ and $2^{nd}$ equation and subtract $3^{rd}$ equation from it, we obtain
$$\begin{align}
OD^2 &= OC^2 + OC_1^2 + OA^2 + OA_1^2 - AC^2 - OB^2\\
&= OC^2 + OC_1^2 + OA^2 + OA_1^2 - (2OA^2 + 2OC^2 - OB_1^2) - OB^2\\
&= OA_1^2 + OB_1^2 + OC_1^2 - OA^2 - OB^2 - OC^2
\end{align}\tag{*1}
$$
Apply parallelogram law and law of cosines to faces $OAB_1C$, $OBC_1A$ and $OCA_1B$, we find
$$\begin{align}
OA_1^2 &= b^2 + c^2 + 2bc\cos\alpha\\
OB_1^2 &= c^2 + a^2 + 2ca\cos\beta\\
OC_1^2 &= a^2 + b^2 + 2ab\cos\gamma
\end{align}$$
Substitute this back into $(*1)$, the desired formula follows:
$$d^2 \stackrel{def}{=} OD^2 = a^2 + b^2 + c^2 + 2bc\cos\alpha + 2ca\cos\beta + 2ab\cos\gamma$$