TL;DR: Doing the review for your supervisor is as mandatory as making him an author of your paper. For the editor, it practically matters only that your supervisor commands the expertise required for the review.
This is kind of circular, but let’s start here:
My advisor had almost no contribution in the paper, but since it is mandatory to put the advisor’s name in the paper, I had to put it.
While I cannot say whether your advisor deserved authorship of your specific paper, it is not globally mandatory to make your advisor an author of your paper¹. Unfortunately, this is often ignored due to the power advisors have over their advisees, making it mandatory locally (i.e., for advisees of a specific advisor).
Now, the fact that you consider it mandatory strongly suggests that it is as mandatory for you to review papers for your advisor and get no credit for it. Only somebody very familiar with your advisor can tell you more on this.
¹ In fact, making somebody an author just because they are officially your advisor goes against academic authorship ethics.
I don’t understand why the journal contacted him and not me to review that paper though I was the corresponding author?
The editor probably asked your advisor to review on basis of the following:
He has expertise in the respective subfield as evidenced by being the last (senior) author on a paper in that subfield.²
He has seniority in academia and thus is an experienced peer-reviewer (possibly evidenced by previous peer reviews for the same journal/publisher), a skilled scientific writer, has a broad knowledge of literature, and can judge the impact of research.
While I wrote the above as facts, the editor can most often only assume these things – they were obviously wrong about the first point in your case.
However, they have to make such assumptions if they want to find any reviewers at all.³
Assuming that this is your first peer review, what should ideally happen in this situation is that you perform the review with your supervisor (or some other experienced peer reviewer) guiding you through the process. There are several mechanisms to register your part in this such as your supervisor officially taking you on board as a reviewer (e.g., APS journals allow this) or your supervisor redirecting the review to you (rejecting and suggesting you as an alternative reviewer).
And now we are closing the loop:
Often this is done unofficially and advisees just perform “mandatory” reviews for their advisor – without getting what little recognition there is for peer-reviewing (and possibly compromising the strict confidentiality of the review).
Editors are aware of all this and may belong to the advisor-takes-it-all school of advising themselves.
And if your advisor does not belong to that school, he can still officially redirect the review to you.
From this point of view, the editor needs not even care whether your advisor actually has the expertise listed above; your advisor only needs to command⁴ this expertise – which he evidently does.
There is another practical aspect to this: Most advisees who published a paper a few years ago have now left academia. While this does not disqualify them to review, they may not care anymore. Advisors on the other hand tend to stay and know the people who can perform the review instead. For example, if your work has been taken over by another advisee who has not published yet, your advisor may perform the review together with your successor.
On the other hand, this way of handling things is detrimental to grooming new reviewers (and knowing that you did).
² The interpretation and importance of corresponding authorship varies across fields, countries, funding agencies, etc. Depending on what applies to the journal’s field, the editor may not even have noticed.
³ Things may be a bit more complicated, as the editor may personally know your advisor, the authors of the paper may have suggested your advisor as a reviewer for the same reasons, etc.
⁴ Along the classic saying: “An undergraduate student knows things; a graduate student knows where something is written; a professor knows somebody who knows.