Is it legal to interchange rows during finding inverse matrix? I'm calculating an inverse matrix using Gauss-Jordan algorithm.
I'd like to use pivoting (with swapping rows, just like in standard Gauss algorithm) in order to avoid division by zero in some cases.
But I also noticed that interchanging rows affects inverse matrix and I don't fully understand how could I avoid division by zero on when running Gauss-Jordan algorithm backwards (when I transform reduced row echelon form of matrix A to identity matrix).

The answer to the asked question is "Yes".
It is most definitely legal to swap rows when finding an inverse matrix.
Perhaps the above statement should be qualified with "by GaussJordan elimination" but I suspect not.
Row swapping is sometimes REQUIRED in the procedure for GaussJordan elimination as described by Gilbert Strang in his lectures and textbooks. My suspicion is that certain matrices REQUIRE row swapping in order to execute GaussJordan elimination no matter what procedure.
The Gilbert Strang references detail the procedure for finding the reduced row echelon format which naturally includes finding the inverse. Row swapping is a completely legal step in Gilbert Strang's references on GausJordan elimination.
Note that swapping rows most definitely changes the inverse of a matrix. Observe the following results in Matlab:
A = [
    1 2 3;
    4 5 6;
    7 8 10;
    ]
Ainv = inv(A)
B= [
    4 5 6;
    1 2 3;
    7 8 10;
    ]
Binv = inv(B)

But the fact that the invesrse of a matrix is not invariant to row swapping is not relevant to the answer of this question.
