The problem here comes from a confusion about what is meant by the word “just.” To do justice to a person, in this context, means to give him at least what he deserves. Thus if I owe a person a favor, it satisfies justice for me to repay him the favor, but this does not stop me from going beyond what justice alone requires and doing him an additional favor.
This has been the standard answer to this question for centuries. St. Thomas Aquinas said, “God acts mercifully, not indeed by going against his justice, but by doing something more than justice; thus a man who pays another two hundred pieces of money, though owing him only one hundred, does nothing against justice, but acts liberally or mercifully.”
The case is the same with one who pardons an offense committed against him, for in remitting it he may be said to bestow a gift. Hence the apostle [Paul] calls remission a forgiving: “Forgive one another, as Christ has forgiven you” (Eph 4:32). Hence it is clear that mercy does not destroy justice, but in a sense is the fullness thereof. Thus it is said, “Mercy exalts itself above judgment” (Jas 2:13). (Summa Theologiae I:21:3)