Federline "is to retain physical custody of the minor children on Wednesday October 3, 2007, at 12pm until further order of the court", the court documents said.
The divorce between Spears, 25, and Federline, 29, became final at the end of July, but their temporary shared custody arrangement for their sons began to unravel amid allegations from Federline's camp that Spears was an unfit mother.
A judge on September 18 ordered Spears to undergo random drug testing in the latest twist in the bitter custody battle over Sean Preston, 2, and Jayden James, 1.
Los Angeles Superior Court judge Scott Gordon said at the time that, according to evidence presented in closed door hearings, Spears was a "habitual, frequent, and continuous" user of "controlled substances and alcohol", court documents showed.
Her drug and alcohol testing was to be "conducted twice per week on random dates and times".
However, Monday's documents said the
transcript of proceedings was "sealed" and gave no details about
what led the court to change the custody order.
Last month, a former Spears bodyguard lodged a statement with the court making allegations of nudity and drug use, and expressing concern about the welfare of Spears's sons.
Reports have said Federline had been seeking a 70/30 arrangement in his favour instead of the 50/50 agreement the couple agreed to after separating.
Spears and Federline were to be "restrained from making derogatory remarks about the other party and the other party's family or significant other", an earlier court order said.
Gordon, who also ordered Spears to see a therapist at least once a week, is requiring her to hire a parenting "coach" and ordered the former couple to sign up for a Parenting Without Conflict program.
Spears filed for divorce last year from Federline, a former back-up dancer and aspiring rap singer, and has rarely been out of the tabloid headlines since - especially following her downward spiral that landed her in a rehabilitation centre in February.
Her stint in rehab followed a series of lurid tabloid headlines chronicling her erratic lifestyle.
She was repeatedly photographed in nightspots wearing no underwear, and was also captured bizarrely shaving her head in a hair salon and attacking a photographer's car with an umbrella.
She reportedly has had fallings out with both of her parents; media reports also speculated that she might suffer from post-partum depression or another psychiatric condition.
The loss of custody was the latest blow to Spears's image as she attempts to rebuild her sagging career ahead of the release in November of her first album in four years.
Just weeks ago, her management company terminated a barely month-old contract with her, following a heavily criticised public performance she gave at the MTV Video Music Awards in Las Vegas.
The talented dancer and performer stunned many at the show by appearing not to know all the moves to her own number, or all the words to her own song.
She shot to superstardom in late 1998, with her smash-hit debut album Baby One More Time which she followed with another chart-topping success the following year, Oops! ... I Did It Again.
Time magazine reports that Spears has sold more than 76 million records worldwide and her 31 million albums sold in the US make her the eighth best-selling female artist in US music history.