Maryland held to its Democratic traditions amid a national Republican landslide last night. Jimmy Carter won the state's 10 electoral votes and the Democrats retained their six congressional seats while sending Republican Rep. Robert E. Bauman, the conservative watchdog of the House of Representatives, to a narrow defeat in his rural district.

With an enormous boost from the state's Democratic heartland, Baltimore City, Carter took a 47-to-44 percent lead over Ronald Reagan with 98 percent of precincts reporting. John Anderson captured 8 percent of the state's vote. Reagan, who led by as much as 12 points in early returns, won suburban Baltimore and Montgomery and Anne Arundel counties, but Carter finished ahead in Prince George's County and was winning by an overwhelming margin of 3 to 1 in Baltimore City.

Maryland, which has voted Republican in only one presidential election since 1960, was one of only a handful of states Carter was winning late last night.

Montgomery County's Rep. Michael D. Barnes, challenged by the Republican he defeated in 1978, and Prince George's Gladys N. Spellman who remained semiconscious after a heart arrest last Friday, won reelection easily. Barnes was trouncing Newton I. Steers by a surprising 59-to-40-percent margin with all but absentee ballots counted, while Spellman beat Kevin Igoe by a 4 to 1 margin.

Liberal Republican Sen. Charles McC. Mathias won a landslide victory over State Sen. Edward T. Conroy, who trailed by about 2 to 1 statewide.

Bauman, with 48 percent of the vote, trailed Democratic state delegate Roy Dyson by four percentage points with 175 of 196 precincts reporting in the 1st District, which was shocked when Bauman was charged with soliciting sex from a teen-aged boy in the District of Columbia and admitted he suffered from alcoholism and homosexual tendencies.

Four other Democratic incumbents -- Reps. Clarence D. Long, Barbara A. Mikulski, Beverly B. Byron and Parren J. Mitchell -- won reelection as expected, and Republican Congresswoman Marjorie S. Holt, whose district includes southern Prince Goerge's, also won easily. s

Carter's victory margin in Baltimore was running far higher than in 1976, when he defeated Gerald Ford in Maryland by winning Baltimore by 97,000 votes. But the President needed the extra cushion, because he fared far worse in most of the rest of the state than he did four years ago. h

In Montgomery County, for example, Carter's narrow 1976 margin vanished with the attraction of Anderson, who won 11 percent of the vote in his state stronghold. Anderson's 30,000 votes more than made up the difference between Carter and Reagan.

In Prince George's, Carter appeared to be winning by a little over half as much over Reagan as he did over Ford. He lead by 18,500 votes with 149 of 150 precincts counted last night.

From the beginning, both sides in the presidential race believed that a Democratic victory would depend on a good turnout in Baltimore, where Carter gained his winning margin over former President Gerald Ford in 1976. In the last weeks, the Carter organization spent most of its campaign budget on phone-bank operations in the city and the printing of sample ballots for the Democratic clubs and ward machines that have always dominated elections there.

The Reagan camp, sensing potential for an upset, invested its funds in radio ads aimed at undecided voters, whose large numbers gave the GOP hope in a state where Democrats have a 3-1 edge in party registration.

But in all, Maryland was largely ignored by the national strategists for both Carter and Reagan, partly because of its small size and partly because of its solid history of voting Democratic in national elections. The presidential campaign in the state was also shoved to the sidelines by the sensational and unexpected troubles of Bauman, and in the last days, by the collapse of Spellman at a Laurel shopping mall.

Carter made one, very brief stopover in Maryland, and Reagan, after skipping the state Republican primary last spring, traveled to Baltimore this fall only for the first presidential debate. Anderson was the only candidate to make a real appearance in the state, and that was for a $50 fundraiser in Montgomery County.

Until the radio campaign of the last week, Reagan's Maryland budget consisted of a few staff salaries and office leases. The Carter forces, leaning on the assumption that Maryland was naturally inclined to vote Democratic, also had scant resources, and was almost invisible in many parts of the state.

In this atmosphere, the field work for the presidential election became a competition between the two state party organizations, which were allowed by federal election laws for the first time this year to raise and spend money to bring out their vote. The state GOP clearly won this contest, starting early with phone banks to identify Reagan supporters and bombarding undecided voters with literature even as the Democrats struggled to raise money.

The Democratic party leaders, discouraged by the outlawing of election day "walk-around" money in Baltimore and some said, by the absence of a serious party challenger to Mathias, never mounted an intensive fundraising drive for field operations, and in fact canceled an event that had been planned to raise $75,000 just before the election.

Both parties enlisted their elected leaders for statewide barnstorming tours, but the Democrats were far more effective in using officials such as Gov. Harry Hughes, Sen. Paul Sarbanes, and Baltimore Mayor William Schaefer for media attention and party morale-building. It was to these elected officials and the neighborhood organizations that respond to them that the Democrats turned to bring out voters for Carter.

Many of them, like Hughes and Schaefer, worked hard, undoubtedly thinking of the wrenches a Reagan administration might deliver to already-strained state and city budgets. Hughes made several trips into the far rural reaches of the state, where his popularity is probably greatest, and even drove to west Baltimore one day and, abandoning his characteristic reticence, knocked on a few doors to show Democratic followers what needed to be done.

Anderson's organization lacked both the funds and the political resources of the two state parties, but did have a large base of volunteers after a summer-long drive to petition the independent's name onto the ballot. As Anderson steadily declined in state polls, even in the Montgomery County suburbs that had once been his stronghold, the state campaign found it harder and harder to gain recognition or help from its card-file of supporters.

At the same time that polls began to show Carter maintaining a lead over Reagan, interest in the state turned to Bauman. Despite calls for his resignation and polls showing him far behind the suddenly formidable Dyson, the 43-year-old Bauman doggedly refused to give up what had been a soaring career and captured attention around the state with his efforts to overcome the twin stigmas of self-confessed alcoholism and "homosexual tendencies."

At times caustically defensive, at others chastened and reclusive, he mailed letters to his constituents explaining that he was a better man for his troubles.