PHOENIX — The Diamondbacks almost got the Dodgers out of town, if not out of mind.

But the Dodgers rallied in the final game between the two teams this season for a 7-6 victory on Thursday behind Hanley Ramirez’s two home runs.

And the Diamondbacks had to watch the Dodgers celebrate clinching the NL West title.

“Maybe this year it’s them,” Arizona’s Martin Prado said, “maybe next year it’s us.”

The Dodgers ran back out from the clubhouse and jumped in the Chase Field swimming pool to celebrate, much to the displeasure of Diamondbacks general manager Kevin Towers and other members of the organization.

But there was little that could be done. It was time for the Dodgers to party, and for the Diamondbacks to contemplate a faded season.

“We were in first place for a long time and let it slip away,” Arizona starter Wade Miley said. “To watch them do that, it is hard to swallow.”

Ramirez, in the lineup after being sidelined five of six games with an irritated nerve in his lower back, played in his 1,090th career game, but this will be his first postseason. Only three active players have played more games without making the playoffs.

“I want to keep going,” Ramirez said. “I think that we’ve got a couple of more steps to do. We’ve got to keep working and stay together all the way through to the end.”

The Diamondbacks could be chasing the Dodgers for some time.

Despite an NL-high payroll of $214 million-plus on opening day, the Dodgers got off to a 30-42 start and were last in the division, 9½ games behind the first-place Diamondbacks, before play on June 22. Los Angeles has gone 58-23 since, including an unreal 42-8 run.

The Dodgers trailed 6-3 in the sixth, and A.J. Ellis hit a tiebreaking homer off Josh Collmenter (4-4) leading off the eighth.

J.P. Howell (2-1) got two outs for the victory.

Ramirez, hit a three-run homer in the third inning and a tying solo shot in the seventh. It was his 14th career multihomer game and first since Aug. 18, 2012.

It was fitting he was the star of the clinching win.

“We’re a veteran ballclub,” Adrian Gonzalez said. “We understand it’s not how you start, it’s how you finish.”

In the ninth, with many Dodgers fans roaring their approval, Kenley Jansen fanned Paul Goldschmidt and Martin Prado before Hill’s fly ball ended it for his 26th save.

Arizona manager Kirk Gibson, whose team won the division two years ago, appreciated what the Dodgers had accomplished, even if it was difficult to watch them celebrate on the Diamondbacks’ turf.

“You just take it in and reflect on some things,” Gibson said. “We still have to play hard the rest of the way out. It is disappointing. We fought hard, and the guys played good today. We were unable to secure the lead.”

With new ownership taking over from Frank McCourt on May 1 last year, the Dodgers stumbled this spring, and Mattingly’s

job appeared to be in jeopardy.

But Los Angeles won 17 of 20 leading into the All-Star break and was 22 games above .500 after a three-game sweep of the Mets on Aug. 17.

Lately, the banged-up team had struggled, losing nine of 12 and five of six entering Thursday’s games.

Los Angeles started strong in this one, though.

Miley’s troubles in the third began when he walked Dodgers starter Ricky Nolasco to lead off the inning. Yasiel Puig was forced out at second on a throw from right fielder Gerardo Parra after the base runner held up on Jerry Hairston Jr.’s blooper. That brought up Ramirez, and he homered to left to make it 3-0.

Nolasco couldn’t hold it.

Arizona started the third with four consecutive hits, including an RBI double from Paul Goldschmidt and a two-run triple by Prado to tie it at 3. Miguel Montero singled Prado home with the go-ahead run. After Parra’s two-out single, Miley’s double to deep left-center brought the runners in to make it 6-3.

Los Angeles came back.

Consecutive doubles by Juan Uribe and Scott Van Slyke, followed by Ellis’ RBI single cut the lead to 6-5 in the sixth, then Ramirez led off the seventh with

his 20th home run of the season, on an 0-2 pitch from Chaz Roe, and it was tied at 6.