Under what authority were the British soldiers accused in the Boston massacre tried by a court of law? I thought British soldiers were not held accountable because they represented the Crown.

I don’t believe British law had any such loophole for soldiers. British troops were expected to obey civil law as well as military regulations. In fact, the mob that was harassing the soldiers was emboldened by the fact that no one had read them the Riot Act, and without that, the soldiers were not permitted to fire their weapons.
Unfortunately the shooting occurred anyway, and the situation could have turned into a full-scale riot. Thomas Hutchinson, a loyalist who was lieutenant governor and acting governor of the province, rushed to the State House to try to calm things down. He promised the local council that justice would be done, and then went out onto the balcony to make the same promise to the angry crowd, which soon dispersed.
Local judges summoned the sheriff and give him a warrant for the arrest of Captain Thomas Preston, the officer who had led seven soldiers to the rescue of a sentry who was being harassed by the mob. By three o’clock in the morning, Preston was in jail, where he would remain for seven months until his trial. The soldiers were also arrested and held in jail. They were tried separately from Preston.
The soldiers were charged under the English laws that prevailed in the colonies in those days. The cases were called Rex v. Preston and Rex v. Wemms et al. In other words, they were charged with offenses against the Crown, i.e., royal authority.
As it turned out, Preston was acquitted, as were most of the soldiers; two were convicted of manslaughter and punished by having a mark branded on the palms of their hand, where the palm meets the thumb.
John Adams, who led the defense, was convinced that the successful defense was one of his greatest accomplishments. He wrote:
“The Part I took in Defence of Cptn. Preston and the Soldiers, procured me Anxiety, and Obloquy enough. It was, however, one of the most gallant, generous, manly and disinterested Actions of my whole Life, and one of the best Pieces of Service I ever rendered my Country. Judgment of Death against those Soldiers would have been as foul a Stain upon this Country as the Executions of the Quakers or Witches, anciently. As the Evidence was, the Verdict of the Jury was exactly right.”