I discovered what happened. I somehow had the SIM PIN protection active in the phone. Also although my issues began just before I upgraded to iOS5, it returned once I upgraded, and thus I believe it may also apply to the problems that some people have had when they upgrade to iOS5 and then get locked out of the SIM card. In other words, they probably are getting locked out of their SIM cards, because, unawares, the SIM PIN protection is already turned on before they do the upgrades.
An Apple help sheet at http://support.apple.com/kb/ht1316 describes the use of the SIM PIN, and somewhere else I read that the phone will require a SIM PIN number if one totally shuts off the phone and then turns it back on again. I found out that with my phone the same thing also occurred, because the battery ran down and the SIM PIN protection was active. Anyway, I had to enter the SIM PIN number, which in my case was also the PUK code, because long ago I made the PIN the same as the PUK. Typically a person should create a PIN that they can remember, and the PUK code only is needed for real emergencies (and is gotten from AT&T) for unlocking a SIM card that has become locked after one has entered a wrong SIM PIN 3 times in a row.
Anyway, since a SIM PIN helps protect the SIM card from being used by a thief, it is best to keep SIM PIN protection activated. If you do not protect the SIM card with a PIN, a thief can put the SIM card into another phone and make unauthorized calls that will use up your minutes on your phone plan.
To turn on the SIM PIN protection, go to Settings > Phone > SIM PIN and select "ON." You will need to enter a code, and if it does not give you a first-time setup menu for creating a PIN, you may need the PUK code from AT&T or Verison. To get the PUK for AT&T, log into your wireless account at att.com, select the tab labeled "MyAT&T," and under that then select the underlined word "Wireless". Next in the information about your account, select the blue underlined item labeled "My Phone/Device", and then in the page that appears, select "Unblock SIM card". A new window will appear that has the PUK code.
Once the SIM PIN protection is turned on, if you have not already set a memorable PIN code, Thus go to Settings > Phone > SIM PIN, and select "Change PIN." Enter a PIN number that you will remember. Also before doing this, be sure you have at hand your PUK code from your carrier, so if you accidently lock the SIM card with too many wrong codes, you can unlock it again.