Massachusetts' highest court ruled Tuesday that the state's ban on civilian possession of stun guns violates the Second Amendment of the U.S. Constitution.

The decision marked a reversal for the court, which reached an opposite conclusion in a different case in 2015. The U.S. Supreme Court, however, set aside that earlier ruling, saying the Massachusetts court failed to properly explain its decision.

The Supreme Judicial Court stayed the effect of Tuesday's ruling for 60 days, leaving the 2004 state ban in place to give the Legislature time to rewrite the law to regulate the ownership of stun guns, without banning them entirely.

"We conclude that the absolute prohibition against civilian possession of stun guns ... is in violation of the Second Amendment, and we order that the count of the complaint charging the defendant with such possession be dismissed with prejudice," the justices wrote.

The defendant, Jorge Ramirez, was charged with illegal possession of a stun gun after a traffic stop in Revere in 2015. He was also later charged with possession of other firearms without the proper licenses.

Ramirez's bid to throw out the stun gun charge on constitutional grounds was turned down by a lower court judge, but later referred to the SJC.

The law prohibits individuals from ownership of any "portable device or weapon from which an electrical current, impulse, wave or beam may be directed," and in manner intended to incapacitate temporarily, injure or kill." The law exempted law enforcement officers, but required police to have special training on the use of stun guns.

Massachusetts is among a handful of U.S. states, including New York, Hawaii and Rhode Island, which ban outright the civilian ownership of stun guns. Many other states have restrictions on sale and possession of the devices.

The earlier ruling in Massachusetts involved the arrest of a woman who told police she carried a stun gun as protection against a former boyfriend. In that case, the SJC upheld the woman's conviction, but the U.S. Supreme Court vacated the ruling. The charge against the woman was later dismissed by mutual agreement but without a further determination of the constitutional questions.

Under Tuesday's ruling, the charge of illegal possession of a stun gun against Ramirez will be dismissed, but the current law remains in effect for 60 days to give state lawmakers the chance to write a new law that could make stun guns subject to the same or similar licensing requirements as other types of firearms.

Messages left Tuesday with Democratic House and Senate leaders, and with a spokesman for Republican Gov. Charlie Baker, were not immediately returned.

Earlier this month, a federal judge in Boston upheld another Massachusetts law that bans assault weapons and large-capacity magazines, ruling that those types of weapons are not protected by the Second Amendment.