Cape Elizabeth scored seven unanswered points in the final 52 seconds, capped by Ethan Murphy’s layup that beat the buzzer, to shock Medomak Valley and win the Class B boys’ basketball state title with a 44-42 victory Friday night at the Cross Insurance Arena.

The Capers won their first state title since 1988, recovering just in time after missing 11 straight shots in the final quarter.

Cape Elizabeth (19-3) trailed 42-37 with 52.2 seconds remaining before freshman Finn Bowe hit a 3-pointer from the top of the key. The Panthers (19-3) then missed a free throw and Jack O’Rourke of the Capers hit a jumper off a Bowe pass to tie it with 24 seconds remaining.

Medomak then missed a layup and Cape rushed down the floor with Eddie Galvin in possession. He passed to a breaking Murphy, who laid it in as time expired.

“Eddie ran the floor and made a perfect pass to me,” said Murphy. “I don’t remember how we got the stop at the other end. I didn’t know how much time was left. I fell to the floor and looked up at the clock and it was 0.00.”

Cape Coach Jim Ray let his players take control in the waning seconds.

“Thank God I didn’t call a timeout,” he said. “Finn Bowe, a freshman, hit the big 3-pointer and then comes back and makes the pass to Jack O’Rourke. We shot terribly. It wasn’t our best game but the kids hung in there. Medomak played outstanding defense.”

Galvin said the ending was like “a dream.”

“I saw Ethan had a step on his man. I think it was something like 10 seconds when I got the ball. I didn’t know if we could win when we were down by five. I had a cramp and went to the trainer. I looked up at the scoreboard and saw we were down by five and I said ‘Oh, my God.’ I wanted to win this so much for my brother, John Messina, who played for Cape and lost twice in the state final.”

Medomak had its chances in the final minute. Nicholas DePatsy missed from the line and moments later, Cameron Allaire, a 6-foot-6 center, missed inside, opening the door for Cape’s miracle finish.

Ray won his first state title after losing in three previous appearances when the Capers were decided underdogs in the final.

“This is something I’ll remember for the rest of my life,” he said.

After Medomak had victory snatched from its grasp, Coach Nick DePatsy was slumped in a chair in the hallway.

“You have to credit Cape’s defense,” he said. “We didn’t get back in transition. The kids might have stood around thinking the clock would run out.

“We missed a foul shot and (Allaire) got in close. I was going to call a timeout just before. It’s a game that could have gone either way. Last week we won a close game (against Winslow in the Eastern Maine final).”

Cape jumped to a 7-0 lead but soon went cold and stayed cold until the last minute. Trailing 9-3, Medomak closed the first quarter with a 9-0 run. DePatsy and reserve Noah Beck accounted for all the points.

Cape Elizabeth shot 7 of 32 in the first half. The Panthers were 6 of 17.

Murphy led Cape with 13 points. O’Rourke had 11, including three 3-pointers.

DePatsy led the Panthers, who were seeking the school’s first state title since 1980, with 14 points. Allaire scored 11 of his 12 points in the second half.