An imaginary morphism from $X$ to $Y$ in a category $\mathcal{C}$ is by definition an equivalence class of spans
$$ \begin{array}{cc} &D& \\ \delta \swarrow & &\searrow{f} \\ X&\to & Y \end{array} $$ where two such spans are equivalent when there is a zigzag of morphisms $(\epsilon,\epsilon')$ making the diagram commutative: $$ \begin{array}{cc} &D& \\ \delta \swarrow & \uparrow{\epsilon}&\searrow{f} \\ X & \cdot & Y\\ \overline{\delta}\nwarrow&\downarrow{\epsilon'}&\nearrow{\overline{f}}\\ &\overline{D}& \end{array} $$ that is $\overline{\delta}\epsilon'=\delta\epsilon$ and $\overline{f}\epsilon'=f\epsilon$ If $\mathcal{C}$ has pullbacks, we can perform the composition of two imaginary morphisms
$$ \begin{array}{ccccc} &&D\times_Y E&&\\ &&\gamma'\swarrow\searrow f'&&\\ &D&&E&\\ &\delta\swarrow\searrow f&&\gamma\swarrow\searrow g&\\ X&&Y&&Z \end{array} $$ The problem I can't solve is to check that that composition is well defined with respect to the equivalence relation between spans. If we assume that $(D,\delta,f)$ is equivalent to $(\overline{D},\overline{\delta},\overline{f})$ via a zigzag $(\epsilon,\epsilon')$ and that $(E,\gamma,g)$ is equivalent to $(\overline{E},\overline{\gamma},\overline{g})$ via $(\eta,\overline{\eta})$, how can we find a zigzag $(\omega,\overline{\omega})$ making $(D\times_YE,\gamma',f')$ equivalent to $(\overline{D}\times_Y\overline{E},\overline{\gamma}',\overline{f}')$? $$ \begin{array}{ccccc} &&D\times E&&\\ &&\gamma'\swarrow\searrow f'&&\\ &D&&E&\\ &\delta\swarrow\searrow f&\uparrow\omega&\gamma\swarrow\searrow g&\\ X&&Y&&Z\\ &\overline{\delta}\nwarrow\nearrow\overline{f}&&\overline{\gamma}\nwarrow\nearrow \overline{g}&\\ &\overline{D}&&\overline{E}&&\\ &\overline{\gamma}'\nwarrow&\downarrow\omega'&\nearrow \overline{f}'&\\ &&\overline{D}\times_Y\overline{E}&& \end{array} $$
First, it seems that you are trying to take equivalence classes by a relation which is not an equivalence relation. If not mistaken, it lacks transitivity: you could have $D$ in relation with $E$ through $(\varepsilon,\varepsilon')$ and $E$ in relation with $F$ through $(\eta,\eta')$ with no possible way to relate $D$ and $F$.
So I will assume that what you mean is that you take the equivalence relation generated by that (here it amounts to take the transitive closure of your relation). Then you have to realize that the set of equivalences classes is actually the connected components of a category. Namely, the span over $X$ and $Y$ forms a category $\operatorname{Span}(X,Y)$ where
Now, two spans are equivalent in the sense above if and only if they are in the same connected component in this category $\operatorname{Span(X,Y)}$.
What you name composition (it actually is a composition in some bicategory but let's forget about his for now) is in fact definable at the level of the categories involved: there is a functor $$ \operatorname{Span}(X,Y)\times\operatorname{Span}(Y,Z) \to \operatorname{Span}(X,Z) $$ given by taking the pullbacks of span as you described it.
So all we have to do is now to prove the following (where $\Pi_0(\mathbf C)$ denotes the set of connected component of a category $\mathbf C$):
Proof. a chain of zigzag $a\leftarrow \cdot \to \cdot \leftarrow \dots \to a'$ in $\mathbf A$ and one $b\leftarrow \cdot \to \cdot \leftarrow \dots \to b'$ in $\mathbf B$ give one $(a,b)\leftarrow \cdot \to \cdot \leftarrow \dots \to (a',b')$ in $\mathbf A\times \mathbf B$ (just add identities on one component if necessary). Hence, by functoriality, it gives one $F(a,b)\leftarrow \cdot \to \cdot \leftarrow \dots \to F(a',b')$ in $\mathbf C$. $\square$
If you want to go further, your construction lies within this more general setting: one can associate to any bicategory $\mathcal B$ the so-called Poincaré category $\Pi \mathcal B$ constructed as follow
Here, you take $\mathcal B$ to be the bicategory of spans in your category $\mathcal C$ to retrieve the result you wanted.