Johnson got knocked down in the first and was nearly finished on two occasions but was able to survive the round.
As it turned out, the failure at finishing him would come back to haunt Castillo.
Johnson caught Castillo with a short left on the chin early in the second that dropped him and from there, he landed a series of crushing right hands from the top to earn the knockout.
The finish of their UFC on FX 5 match came at 1:06 of the second.
"The left, I caught him slipping," Johnson said. "It was just a good shot. I hit him with the left and saw his eyes roll in the back of his head so I couldn't let him get up."
It was the third straight win for Johnson, who is now 12-6. He said afterward that he was gunning for a top 10 spot in the stacked lightweight division.
Castillo saw a three-fight win streak snapped in falling to 14-5. He nearly finished Johnson with strikes and then looked close to closing it out with an arm triangle in the first, only to see Johnson survive and stun him shortly thereafter.
"He caught me in the first round," Johnson said. "Hats off to him. I just had to stay composed. He wasted all his energy. My hands got the best of him and that's a wrap."