Four years ago, Paul Pierce and Kevin Garnett played their final games for the Celtics. A month later, they were traded to the Brooklyn Nets in a massive deal that involved multiple first-round picks going to Boston.

On Tuesday, the Celtics reaped the ultimate reward of acquiring a first overall pick by winning the NBA draft lottery. Boston will select first for the first time since the lottery began in 1985.

It is the second time the Celtics will pick first. The other time was 1950 when Boston picked Chuck Share, who never played a game for the team.

“I’m glad we didn’t trade it, you can write it down,” Boston managing partner Wyc Grousbeck said.

The Celtics obtained the pick from the Nets in 2013 and had a 25 percent of getting the top pick. The Nets finished with the league’s worst record at 20-62 under first-year general manager Sean Marks, who replaced Billy King in February 2016.

When the Celtics traded Garnett and Pierce after the duo played six seasons together and won the NBA title in 2008, they began a rebuild. The Celtics won 25 games in the first season after the trade but their win total has improved in each of the next three seasons, culminating in getting the top seed in the Eastern Conference this season.

The Nets went the opposite way. They won 44 games and lost in the second round to the Miami Heat in the only season Pierce and Garnett played together in Brooklyn.

Pierce signed with the Washington Wizards in free agency and Garnett was traded to the Minnesota Timberwolves at the 2015 deadline for Thaddeus Young. In 2015-16, the Nets lost 61 games and fired King along with coach Lionel Hollins in January.

“Back in 2013 it felt like a fair deal for both sides,” Grousbeck said. “This is what Brooklyn wanted to do, wanted to go all on it, chase a championship. They put together really a heck of a team, a great team. Now it’s our turn four years later to take some benefits from the trade.”

Besides getting the first overall pick a night before they open the Eastern Conference finals at home against the Cleveland Cavaliers, the Celtics also get another first-rounder in 2018 from the Nets. Boston already obtained first-round picks from Brooklyn in 2014 and 2016.

“That felt pretty good,” Grousbeck said. “It felt like winning Game Seven last night. There’s a lot of good feelings around the Celtics right now.”

After the lottery ended, Pierce wrote on twitter: “And look what I leave behind for the Celts on my way out #1 pick.”

Markelle Fultz is projected to be the top overall pick, and the Celtics could use the pick to pair him in the backcourt with Isaiah Thomas. Both players went to the University of Washington. Thomas was the last overall pick by the Sacramento Kings in the 2011 draft.

While Boston was going to get a top-four pick, the Los Angeles Lakers had to sweat out the lottery. They moved up one spot to No. 2, but if the pick had fallen outside the top three, the Philadelphia 76ers would have received the selection.

The 76ers, who used the top overall pick in Ben Simmons last year, will select third. It is the third straight season Philadelphia is in the top three as part of a rebuilding process started by former GM Sam Hinkie telling fans to “Trust the process.”

“Tonight was a great night as we continue to build our team into a contender,” Sixers president of basketball operations Bryan Colangelo said. “There really was no way to walk out of here disappointed given the possible outcomes we faced. We’ll now zero in and focus on drafting a quality player at No. 3 who adds to our dynamic young core. We also now have clarity that we’ll have a 2018 unprotected pick from the Lakers.”

Phoenix will get the fourth overall pick in its eighth straight lottery appearance.

The Sacramento Kings rounded out the top five. The Orlando Magic and Minnesota are sixth and seventh while the New York Knicks are eighth.

The Dallas Mavericks are ninth and the New Orleans Pelicans are 10th. New Orleans traded the pick to Sacramento in the deal involving DeMarcus Cousins at the trade deadline.

The Charlotte Hornets, Detroit Pistons, Denver Nuggets and Miami Heat will get the final four lottery picks.