President Carter and Sen. Edward M. Kennedy were in a virtual dead heat in returns from the Pennsylvania primary tonight, with Kennedy's hope of scoring a decisive breakthrough of even a narrow victory in jeopardy.

It appeared that the president, who trailed Kennedy throughout the night, was in a position to squeeze out a come-from-behind win as the late returns were counted.

As the votes trickled in from rural areas where Carter ran strongest, Kennedy's margin, once more than 10 percentage points, dwindled.

With 71 percent of the precincts counted, Kennedy and Carter each had 46 percent of the vote.

California Gov. Edmund G. (Jerry) Brown Jr., who has abandoned his campaign but whose name remained on the ballot, had 2 percent.

The Pennsylvania ballot also allowed voters to express "no preference," and 6 percent of the Democrats took this option.

"The three television networks, which conducted exit polls in key precincts across the state, had said they expected the Massachusetts senator's margin to dwindle as returns from rural and western Pennsylvania came in.

Kennedy's assessment of the result changed during the course of the night as his lead slowly faded. At 11:30 p.m., he told a cheering chanting crowd of campaign workers in Philadelphia that "it appears like we have a narrow victory." Ninety minutes later, when his campaign plane landed at National Airport in Washington, he said he was still "confident" but would "wait for the official returns before making any definite comment."

The closeness of the race indicated that Kennedy and the president would about split the 126 convention delegates at stake. This would work to the advantage of Carter, who holds a substantial lead in delegates chosen in earlier primaries, a lead Kennedy is struggling to overcome.

But psychologically, Pennsylvania was crucial to Kennedy, who is now basing his long-shot strategy on putting together a string of primary wins in the late industrial states, beginning here. Even the narrowest of victories in Pennsylvania would be welcomed by Kennedy aides. But they clearly also hoped for a decisive margin that would allow them to claim that Pennsylvania marked a fundamental change in the Democratic presidential race.

In 1976, it was in Pennsylvania that Carter made his breakthrough to the Democratic nomination. But tonight the president failed to score the decisive knockout he was looking for, probably assuring a protracted and increasingly bitter fight with Kennedy all the way to the August Democratic convention. Such a fight would weaken the eventual nominee's chances in the fall.

An additional 59 Pennsylvania delegates to the Democratic National Convention will be chosen by the state party in June.

With the Pennsylvania results uncertain, Carter campaign aides tonight quickly pointed to the results of todays Democratic caucuses in Missouri, where they said the president had won at least 60 of the state's 77 delegates. They did not mention the results in the Vermont Democratic caucuses, also held today, where Kennedy was leading.

As the Pennsylvania votes were being counted, Carter's national campaign chairman, Robert S. Strauss, told the president's supporters that the final returns would be "very, very close." Noting Kennedy's intensive campaigning here the last three weeks, Strauss added, "This will be no victory for him, I can promise you that."

Strauss' assessment revived the subdued Carter backers.

In Washington, White House press secretary Jody Powell said, "If I had to guess, I'd say we'd lose by one, two or three points, but that's an improvement from last week when we were doing a damn sight worse than we told people we were."

A Washington Post poll of voters as they left polling places showed that the nation's mounting economic problems seriously hurt the president in Pennsylvania, as Kennedy had hoped.

More than half of those interviewed said Carter was not handling the problem of inflation well, and these voters went for Kennedy by more than 2 to 1. Carter won overwhelmingly among voters who believe there is not much any president can do about inflation.

But on what is clearly the dominant foreign policy issue of the campaign -- the crisis in Iran -- the president more than held his own, scoring decisive majorities among the more than 60 percent of the voters in the poll who believe he has handled the crisis as well or better than other presidents would have.

However, in an indication of voter impatience with the prolonged Iranian stalemate, more than a quarter of those Democrats polled said Carter's handling of the crisis has been worse than they would expect from other presidents.

Despite a "no preference" choice on the ballot, which Kennedy strategists said made Pennsylvania a clear-cut test between the two candidates and not a vehicle for protest votes against either, the Post poll showed that negative voter feelings played a significant role in the contest.

More than 20 percent of the voters polled said they supported Kennedy or the president chiefly because of disapproval of the other man. These negative factors could work against either candidate in a general-election campaign against Ronald Reagan, the likely Republican nominee.

The Post poll showed Kennedy scoring a big win in Philadelphia, as expected. It also showed that Kennedy won narrowly among labor union members and their families, and ran strong among his fellow Roman Catholics, which often has not been the case in earlier primaries.

Carter's strength, the poll indicated, was among women and young voters.

These findings generally conformed to the results of similar polls by other news organizations. An Associated Press-NBC News poll indicated that almost two-thirds of Democratic voters here do not believe the president is doing all he can to fight inflation, and that these voters went strongly for Kennedy.

An ABC poll showed a 2-to-1 Kennedy lead among voters who considered the economy the country's main problem. But among the smaller number of voters who said the Iranian crisis or the Soviet Union's invasion of Afghanistan was the most important issue, the president was the clear winner.

Geographically, the ABC poll said Kennedy won easily in Philadelphia and its suburbs and also had majorities in the smaller industrial cities of eastern and western Pennsylvania. The polls said the two candidates split the vote in the Pittsburgh area, and that Carter won by about 2 to 1 in the more rural, central section of the state.

Kennedy began pointing toward Pennsylvania immediately following his upset victories last month in the New York and Connecticut primaries.

Despite subsequent losses to Carter in the April 1 Wisconsin and Kansas primaries, Kennedy contended that Pennsylvania would mark a decisive turning point in the Democratic race, where he would begin gaining ground on the president on his way to winning the nomination at the convention in New York in August.

As a result of this strategy, Kennedy concentrated his campaigning during the last three weeks in Pennsylvania, a large industrial state with almost 3 million registered Democrats. Party crossover voting -- a factor in Wisconsin and some other states -- was not allowed here.

Kennedy also hoped that economic issues, the basis of his challenge, would find fertile ground in this heavily unionized state, where concern over inflation and the likely impact of the recession on the steel industry runs high.

Crisscrossing the state, Kennedy hammered away at administration economic policies. At one of his last campaign apperances in the state on Monday, he seized on the president's prediction at a news conference last week that the recession would be "mild and short."

"Well, let's hope it's a really mild one," Kennedy shouted at a campaign rally. "We hope that only one person is going to lose his job, and that's going to be Jimmy Carter."

The Carter campaign countered the Kennedy drive by pouring surrogates such as Vice President Mondale into the state and increasing its spending for advertising.

Mondale, carrying the brunt of the Carter campaign, stressed the administration's efforts to aid the steel industry and accused Kennedy of political opportunism in criticizng White House policy on steel imports.

"This administration has delivered for steel, will continue to deliver for steel, and the test for the voters of Pennsylvania is whether you like an administration that has worked for steel for 3 1/2 years or a candidate who just discovered the problems of steel three weeks ago," Mondale told a Pittsburgh audience.

But Kennedy's emphasis on economic issues, coupled with growing voter impatience, over the stalemate in Iran, took a toll on the president in the weeks before the primary, Carter aides acknowledged.