A New Jersey roofer, faced with going to jail for contempt, agreed yesterday to testify before a federal grand jury that is believed to be seeking the identity of the "shark" whose tampering efforts led to a mistrial in the first political corruption trial of suspended Maryland Gov. Marvin Mandel.

Benjamin Greenberg, 60, of Cherry Hill, N.J. told U.S. District Court Judge Alexander Harvey II that today he will answer questions by the special guard jury that reopened the investigation into the circumstances that led to the mistrial on Dec. 7, 1976.

Mandel and five codefendants were convicted after a second trial and Mandel was replaced as active governor. The suspended governor and the five others are free on bail awaiting a ruling, expected soon, from the 4th U.S. Circuit Court of Appeals in Richmond.

Greenberg twice refused to testify last month, contending that questions posed by federal prosecutors were based on information obtained by illegal wiretaps. Among conversations intercepted by electronic surveillance, according to Greenberg's attorney, James T. Vernile, were some between Greenberg and Charles Edward Neiswender, the self-styled con man from Cinnaminson, N.J., who was one of two men convicted of jury tampering following the mistrial.

After listening to arguements by Vernile and Barnet D. Skolnik, the chief prosecutor in Mandel's political corruption trials, Harvey ordered Greenberg to testify or face the prospect of going to jail for as long as 18 months.

After meeting with Skolnik in a closed session, Judge Harvey ruled that Greenberg had produced no evidence to support his allegation that he had been illegally tapped. The judge added that Greenberg had no right to challenge questions put to him on the basis of information gathered by surveillance of Neiswender and others.

During Neiswender's trial, Arnold M. Weiner, Mandel's chief attorney, testified that after someone telephoned him offering to fix the trial for $20,000, he contacted federal authorities. A postal inspector posing as an associate of Weiner, recorded an in-person conversation with Neiswender that led to his conviction.

At the time, Neiswender said he was acting as an intermediary for someone connected with Maryland politics whose identity has never been uncovered. Neiswender also remains free on bound pending the outcome of his appeal.

The other person convicted in the tampering, Walter E. Weikers, tesified before the grand jury earlier this month. Weikers, a 69-year-old Baltimore furniture salesman, was released from prison Sept. 1 after serving 17 months of a two-year sentence. Weikers was convicted in February 1977 of offering $10,000 to a juror, Oscar Sislen, a relative by marriage, to hold out for acquittal.