I will start with a very short discussion of convolution using the following image from Wikipedia:

As shown, the convolution of two one-dimensional functions involves the reflection of one of them (i.e., the convolution kernel) that shifts the two functions one above the other and calculates the integral of their product.
When folding two-dimensional matrices, the convolution kernel is reflected in both dimensions, and then the sum of the products is calculated for each unique overlapping combination with another matrix. This reflection of the size of the core is an essential step in convolution.
However, when performing filtering, we like to think about the filtering matrix, as if it were a “stencil” that is directly stacked as is (i.e. without reflections) over the filtered matrix. In other words, we want to perform the equivalent operation as a convolution, but without reflecting the dimensions of the filter matrix. To cancel the reflection performed during the convolution, we can add an additional reflection of the dimensions of the filter matrix before the convolution is performed.
Now, for any given 2-D matrix A , you can prove to yourself that turning both dimensions is equivalent to rotating the matrix 180 degrees using the FLIPDIM and ROT90 functions in MATLAB:
A = rand(5); %
This is why filter2(f,A) equivalent to conv2(A,rot90(f,2),'same') . To further illustrate how there are different representations of filter matrices compared to convolution kernels, we can see what happens when we apply FILTER2 and CONV2 to the same set of matrices f and A , which is defined as follows:
>> f = [1 0 0; 0 1 0; 1 0 0] %
Now when executing B = filter2(f,A); the calculation of the output element B(2,2) can be visualized by aligning the central filter element with A(2,2) and multiplying the overlapping elements:
17*1 24*0 1*0 8 15 23*0 5*1 7*0 14 16 4*1 6*0 13*0 20 22 10 12 19 21 3 11 18 25 2 9
Since elements outside the filter matrix are ignored, we can see that the sum of the products will be 17*1 + 4*1 + 5*1 = 26 . Note that here we simply put f on top of A as a “stencil”, namely, how the filter matrices are perceived to work with the matrix.
When we execute B = conv2(A,f,'same'); , the calculation of the output element B(2,2) as follows:
17*0 24*0 1*1 8 15 23*0 5*1 7*0 14 16 4*0 6*0 13*1 20 22 10 12 19 21 3 11 18 25 2 9
and the sum of the works will be 5*1 + 1*1 + 13*1 = 19 . Note that when f taken as the convolution kernel, we must flip its dimensions before putting it on top of A