Ahh yes. I threw my math at it and I think I hit it. You're correct it does involve the Pythagorean theorem and some scaling.
You start with your normalized vector that represents your ray.

It has an x component and a y component. First we want to see how long it is when it travels one unit in the x direction. So what do we do? We want to scale the entire vector so that the x component equals 1. To figure out what to scale it by, we do the following:
scaleFactor = 1/rayDirX;
Writing that out in math it's really just
scaledX = rayDirX * (1/rayDirX) = 1
So we can just call that 1.
Then for the y component:
scaledY = rayDirY * (1/rayDirX) = rayDirY/rayDirX
So now we have our scaled components as (1, rayDirY/rayDirX)
Now, we want to know the length. Now Pythagorean comes into play. Which is
length = sqrt((x * x) + (y * y))
So plugging in our scaled components we get:
length = sqrt((1 * 1 ) + (rayDirY / rayDirX) * (rayDirY / rayDirX))
Apply some algebra and simplify and we get:
length = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX))
Same goes for the length when the y component travels one unit, except we'll have (rayDirX/rayDirY, 1) which results in
length = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY))
There we have your two equations from your question. Pretty neat. Thanks for the algebra exercise.