ORONO — Heading into the 400 freestyle relay, Greely High had yet to win a race. The Rangers were runners-up seven times and didn’t have to win the last event to clinch the state title.

Never has second place felt so fine.

Greely placed second in the 400 free relay, and that was good enough to win the Class B girls’ swimming and diving state championship by a half-point Monday at the Stanley M. Wallace Pool at the University of Maine.

The Rangers finished with 350.5 points – to Mt. Desert Island’s 350 – to win their first title since 2012.

Two-time defending champion MDI, with four individual and three relay victories, placed second. Cape Elizabeth (181) finished third.

Before the final event, Greely led MDI by 6.5 points. In the 400 freestyle relay, MDI won in 3:46.26, collecting 40 points. Greely was second (3:52.92), holding off third-place Morse (3:56.12) to earn 34 points.

“We swam great. They swam great,” Greely Coach Rob Hale said. “No one should be hanging their head. That was an amazing meet.”

Greely was led by Hwanhee Park’s two second-place finishes in the 200 IM and 100 butterfly. Cat Maker placed second in the 50 free and third in the 100 free; Katie Bacall was second in the 500 free and fourth in the 200 free; and Kate Dransfield placed second in the 100 backstroke and fourth in the 50 free.

All three Greely relay teams placed second to MDI. Bacall anchored the crucial 400 free relay.

“I was really nervous because Morse was right with us at the start,” said Bacall.

But Camilla Civiello kept Greely in it, and Lily Black and Lauren Williams gave the Rangers a five-second lead.

“At that point I knew we could definitely do it,” said Bacall, who held off Morse’s Ann Tolan.

“You can’t get any closer unless you tie,” MDI Coach Tony DeMuro said. “Across the board, we swam great.”

MDI collected the gold medals, led by Leila Johnston, a senior headed to Florida State. She was named performer of the meet after setting state records in the 200 free (1:50.63) and 100 free (51.02). She also swam on the 200 medley and 200 free relays (meet-record time of 1:39.46).

Johnston’s teammate, sophomore Lydia DaCorte, was also a double-winner, taking the 100 butterfly (58.69) and 500 free (meet-record 5:08.45).

McAuley sophomore Ana Neff-Jendrasko brought home two first-place medals, in the 200 IM (2:12.60) and 100 back (1:00.52).

Morse’s Tolan, a sophomore, won the 50 free (24.35) and placed second to Johnston in the 100 free (53.11).

Cape Elizabeth senior Sadie Stiles won the 100 breast stroke in 1:07.95, edging North Yarmouth Academy junior Sonia Lin (1:08.13).

Old Town sophomore Kassidy Harriman won diving (311.65 points).

MDI began the meet with an expected win in the 200 medley relay (1:53.09). Greely’s Maker swam a strong anchor freestyle leg (24.73), moving the Rangers up to second place (1:54.33). Dransfield, Park and Madison Rawnsley swam before Maker.

Johnston beat the old state record in the 200 freestyle by 1.35 seconds. But the Rangers placed three in the top six – Bacall (fourth, 2:04.79), Black (fifth, 2:07.86) and Civiello (sixth, 2:08.43).

Park gave Greely more points with her second place in the 200 IM, and so did Allison Coon (14th) and Rawnsley (16th).

While Morse’s Tolan was winning the 50 free (24.35), Greely scored four swimmers – Maker (second), Dransfield (fourth), Hannah Butland (tied for 12th) and Ciara Hayes (14th). Butland’s and Hayes’ effort were huge. They were seeded 17th and 20th, and provided an extra 7.5 points.

Heading into the diving break, the Rangers led MDI 136.5-109. MDI diver Chelsea Schroeder placed second to Harriman. Cape Elizabeth senior Michaela Pinette was third. Greely didn’t have a diver.

After DaCorte’s butterfly win, Johnston blistered the 100 free, with three teammates also scoring. MDI took the team lead, 212-208.5.

DaCorte easily won the 500 free, good for 20 more points. But Greely scored 45 points in the race, with Bacall second (5:39.56), Black fourth (5:51.82) and Meagan Currie fifth (5:51.96). Greely led, 253.5-232.

MDI clawed back, setting up the exciting finish. Greely teammates stood on one side of the pool, and MDI swimmers on the other..

And the Rangers hung on, barely.