The reason this works is that the integral is the anti-differential. Just like you can differentiate both sides, you can do the reverse process and integrate both sides.
Let's look at a simple example:
$x^2 = \sin(y)$.
We can differentiate both sides and get:
$$2x\,dx = \cos(y)\,dy$$
A lot of people want to differentiate "with respect to a variable" but that's actually unnecessary. The "with respect to a variable" part just means dividing by that differential after-the-fact. So, if we differentiated "with respect to q", then we would just divide both sides by $dq$:
$$2x\,\frac{dx}{dq} = \cos(y)\,\frac{dy}{dq}$$
But, there is nothing special about "with respect to" a variable. It just means that after differentiating, we will divide by the differential of that variable.
So, if the differential of our equation is just $2x\,dx = \cos(y)\,dy$, then, we can go the other way by anti-differentiating (also known as integrating):
$$ \int 2x\,dx = \int \cos(y)\,dy \\
x^2 + C_1 = \sin(y) + C_2 $$
It's just the reverse process. And, like differentiating, it doesn't require that you integrate "with respect to" any variable. It just requires that the forms match.
Here's another example that's a little more trippy:
$$ \int (x\,dz + z\,dx) = \int dy $$
Here, we can see the left-hand side is the result of the product rule. So, this becomes:
$$ xz + C_1 = y + C_2 $$.
This all works because we are performing the exact same operation to both sides - anti-differentiation. Since neither differentiation nor integrations needs to be "with respect to a variable", it is essentially "symmetric" to both sides, even when they are using different variables.