Cejudo (13-2) narrowly edged Johnson (27-3-1) via split decision. Two judges scored the contest for Cejudo 48-47, and a third saw it for Johnson 48-47.
"To defeat the man, the myth, the legend, Demetrious Johnson, it feels good," Cejudo, 31, said after the fight.
The loss comes only nine months after Johnson set a UFC record with 11 consecutive title defenses. His submission victory over Ray Borg at UFC 216 in October broke former middleweight champion Anderson Silva's mark of 10.
A U.S. Olympic gold medalist wrestler in 2008, Cejudo's historic victory began with an awkward moment. His left leg buckled underneath him during the opening minute, as he bounced on his toes. Cejudo switched to the southpaw stance for most of the first round, which signaled a possible injury.
He returned to his traditional orthodox stance from the second round on and did not appear bothered the rest of the way. He converted five of 14 takedown attempts, which played a key role in the decision. Johnson bounced back to his feet after the majority of them, but Cejudo had success holding him down in the fourth.
According to Fightmetric, Johnson outlanded Cejudo in total strikes 110-71. The majority of his strikes were kicks to the leg, which appeared to have an effect on Cejudo but only a minimal one. Cejudo seemed to get more aggressive and confident as the fight progressed, and he kept Johnson honest with a quick counter right.
Johnson's defensive wrestling was mostly up to the task, but the time he spent on his back in the fourth round proved crucial. He applauded Cejudo as the result was read, and UFC president Dana White put the belt around Cejudo's waist.
"This is a dream come true, from Olympic gold medalist to UFC champion,'' Cejudo said. "I was born right here in Los Angeles in a two-bedroom apartment. So from the bottom of my heart, thank you to these fans in California for their support.''
Saturday's fight was a rematch of UFC 197 in April 2016 in Las Vegas. That night, Johnson defeated Cejudo by first-round TKO. Cejudo had been a professional fighter for only three years at that time.
"I hate second place with a passion," Cejudo said. "And losing to Demetrious Johnson the first time, in a devastating fashion, not only motivated me but put a chip on a shoulder."
Immediately after, Cejudo, who fights out of Scottsdale, Arizona, called for the winner of Saturday night's bantamweight title fight between TJ Dillashaw and Cody Garbrandt; Dillashaw won by TKO in the first round. It was a notable move by Cejudo in that Johnson, whom most expected to win, recently said he was done talking about a potential move up in weight or a superfight of any sort.
After Dillashaw defeated Garbrandt, he was asked about Cejudo's call-out: "Bring it, baby, let's do this," he said.
Johnson, of Kirkland, Washington, was the only champion in the history of the UFC's flyweight division until Saturday. His last defeat came at 135 pounds in October 2011, against then-UFC champion Dominick Cruz.
Information from the Associated Press was used in this report.