Why the curve getting twisted
1) If you first apply the rotation to mesh-plane and converts into a curve then generally this problem happens.
So, if you only need a rectangular curve object, then the best way to ADD a curve-circle > EDIT mode > Press V > set handle type to 'Vector', you have rectangular curve object.

Now the twist problem:-

2) For circular curve or curve having smooth bends: Go to Curve properties panel, then change the 'twisting' option from 'minimum' (which is default) to 'tangent' and for a curve having smooth bends, also adjust the 'smooth' option accordingly. (Note- Spline Type should be 'bezier')

When the 'twisting' is set to 'minimum' :


When the 'twisting' is set to 'tangent' :


3) For rectangular curve :

(a) Rectangular curve from curve-circle: ADD a curve-circle > EDIT mode > Press V, set handle type to 'Vector' > then again Press V set handle type to 'Free', then go to Curve properties panel, then set 'twisting' option to 'minimum' and 'smooth' to 50 (adjust accordingly). In this way you have a twist free curve even if you move curve-points in the edit-mode and movement of curve-points only shift the overall tilt of the curve. (Note- Spline Type should be 'bezier')



(b) Rectangular curve from mesh-plane: If you convert mesh-plane to curve object and it has Spline-Type 'poly', therefore twisting option does not work properly. So, go to Edit Mode > Tools > Curve Tools > Set Spline Type to 'Bezier' > Press V, set handle type to 'Vector' > then again Press V set handle type to 'Free'. Then go to Curve properties panel, then set 'twisting' option to 'minimum' and 'smooth' to 50 (adjust accordingly). (Note- Spline Type should be 'bezier')