In good literature there is always versimilitude. That is, there is a likeness to the truth, to reality. In order to establish this verisimilitude, a writer must establish in the exposition of his story for the results of the denouement.
In O. Henry's "The Gift of the Magi," for instance, the exposition establishes the penurious condition of the couple:
The 'Dillingham' had been flung to the breeze during a former period of prosperity when its possessor was being paid thirty dollar per week. [Good money in the early 1900s]
Della counts her change--$1.87--and sobs because she so wants to buy her dear husband a present. Although he does not earn as much, she is still proud of him and loves him dearly:
...whenver Mr. James Dillinngham Young came home and reached his flat above he was called 'Jim' and greatly hugged by Mrs. James Dillingham Young.
That the young couple would be willing to sacrifice their prized possessions for the love of the other-"foolishly," as O. Henry declares their actions--is entirely plausible. Della catches herself in the mirror and realizes that she can obtain the money for her husband's gift. Likewise, Jim who is so proud of his gold watch fob, is willing to pawn it for a gift for his wife. His reaction when he sees Della with her shorn locks demonstrates his love for her as he does not become angry and he makes no complaint as he throws the package onto the table. Calmly, he tells her to open the package and she will understand. When she sees the contents, Della does not complain; instead, she comforts her husband," My hair grows so fast, Jim!" Similarly, Jim consoles Della after seeing his "dandy" watch fob:
...let's put our Christmas presents away and keep 'em awhile. They're too nice to use just at present. I sold the watch to get the money to buy your combs. And now suppose you put the chops on.
Unselfish and loving in the beginning, Della and Jim are as unselfish and loving in the denouement. Indeed, there is verisimilitude to O. Henry's story, and the surprise ending is certainly reasonable, not to mention heartwarming. As the critic Rena Korb writes,
Foreshadowed in the opening are the sacrifices that Della and Jim will make for each other. Della shows herself already acquainted with saving and scrimping--elements of sacrifice; her ability to withstand the approach of the vendors highlights her ultimate willingness to give up something she values highly--her hair--for that which she values more: the love of her husband.
I don't think the ending of this story is a trick. In fact, I think it's just about inevitable given the way the story unfolds and the situation as it is set up.
If Jim hadn't sold the watch, there would hardly be any point to the story. It wouldn't be very powerful because it would just be a nice story about some woman who sacrificed something so her husband could have something nice.
But for it to turn out the way it is seems rather logical. It makes the story much more poignant and it makes the two both be equal in their sacrifices for each other.
So I think this is much more logical than it is a trick.