Sen. Edward M. Kennedy (D-Mass.) departed icy New England tonight for a brief respite in warmer regions, but he left behind here an intriguing mystery: how can a man who's so far behind be so happy?

Kennedy's campaign staff expects him to lose to President Carter in the Maine caucuses Sunday, and Kennedy people are glum about their chances in the New Hampshire primary Feb. 26. But the candidate is in excellent spirits.

He is more relaxed and humorous on the campaign trail now than at any time since the first days of his campaign last November. He jokes with voters, staffers, and reporters about his campaign, and he is putting in grueling days with a zeal that would seem to belie those who say his heart is not in his race for the presidency.

What's going on? It is partly that Kennedy is taking a philosophical, long-range view of the election that reaches beyond the voting in New England this month.

"I have a sense, I have a feeling about this that's just a lot different from what all you guys are writing," Kennedy said to a reporter the other day. "This isn't going to end after New Hampshire. This thing's got months to go yet." Kennedy's political advisors now recite the same line. Three months ago, they expected their man to tromp Georgia's Jimmy Carter with the speed and finality of Sherman's march to the sea. Now, the Kennedy refrain is borrowed from Gen. Grant at Richmond: "I propose to fight it out on this line if it takes all summer."

The key to this strategy is money. Kennedy's advisers say they can lose New England and stay in the race, waiting for a decline in Carter's popularity surge -- a decline they consider inevitable -- if they can come up with some campaign funds.

"At this point, the votes aren't as important as the money. That's the key," says Lawrence Horowitz, who doubles as the campaign physician and a chief political adviser.

That's why Kennedy left here today for a day in Washington to be followed by trips to Florida and Puerto Rico at the end of the week where liberal Democrats will gather for Kennedy fund-raisers.

Another reason for Kennedy's good mood in the face of bad news is that he seems to like the role of underdog. The metamorphosis from frustrated, tense candidate to relaxed, feisty challenger began on Jan. 11, the day an Iowa poll first showed that Carter was about to Clobber Kennedy.

Kennedy now seems at his best when he acknowledges his position as a fairly distant challenger. When he toured a factory in New Hampshire last week, Kennedy, who is not usually adept at small talk, was stilted in his greetings until he met a young worker who challenged him for running against Carter at a time of international crisis.

Kennedy proceeded to have a long, friendly exchange with the young man. As he walked away he turned around and said, "Now that stuff is interesting."

Similarly, Kennedy comes to life in campaign appearances when he takes on the issue of Carter's refusal to debate. Sometimes he makes fun of Carter; sometimes he blasts away. But every time he makes the point with obvious relish.

An issue that Kennedy does not relish, but feels obliged to bring up at every stop, is gun control. He is worried about advertisements being placed by rifle groups here that described him as the gun owner's chief public enemy.

Kennedy says he would ban the manufacture or importing of "Saturday night specials" and other handguns that have no sporting use. But he insists that he would not favor legislation on "long guns."

On this issue, as on abortion, Kennedy's position is virtually identical to the president's. But the gun owners, like many anti-abortionists, are focusing their attack on Kennedy.

Kennedy has not raised Chappaquiddick in his New England speeches over the past week, but he says he will repeat the television commercial in which he admits he was at fault in that tragedy and asks for people to deal "fairly" with him.

As he did in Iowa, Kennedy has drawn big and apparently enthusiastic crowds both at public speeches and private-home coffees in Maine and New Hampshire.