LeBron James, Dwyane Wade, Carmelo Anthony and other NBA all-stars joined Obama in Washington to entertain wounded troops.
The present-day stars were joined by some retired legends, including Bill Russell and Magic Johnson. College player Maya Moore of the Connecticut Huskies women's team also played.
The game was played for a group of "wounded warriors" - troops injured in action - and participants in the White House's mentoring program. It took place at a gym inside Washington's Fort McNair, a short drive from the White House. The president was inside the gym for about two hours.
The reporters assigned to keep tabs on the president were shut out of the gym, forced to wait in vans outside. Asked why media coverage was blocked, White House Press Secretary Robert Gibbs said Obama "just wanted to play."
After the game some of the players joined Obama and a group of his friends for a barbecue at the White House, capping the president's 49th birthday week.
A small tent and tables decorated with sunflowers and yellow and white tablecloths were being set up on the South Lawn when Obama returned from playing basketball. The White House said the meal will include shrimp from the Gulf of Mexico.
First lady Michelle Obama and daughter Sasha returned to the White House on Sunday night after a vacation in Spain. The Obamas' older daughter, Malia, is away at summer camp.